Thread & ProgressBar

Ok. I know this is a wierd question. But I would like to know how to connect a progressbar to a thread. I have a download function, and when the user clicks a button, the function is a thread you could say. So basically, I would like to track the progress of the download or thread in a progressbar. Here is my thread.

 

Button_Click Event(Object Sender, EventArgs e)

{

          Thread MyThread = new MyThread(Downloader);

          MyThread.Start()

 

}




Answer this question

Thread & ProgressBar

  • A kid

    That's not at all a weird question!

    If you're using VS2005, the easiest way is to use the new BackgroundWorker class:

    See http://msdn2.microsoft.com/en-us/library/hybbz6ke.aspx

    However, if using earlier versions that class doesn't exist. You'll have to add event callbacks to your thread to report progress, and then you'll have to make sure the event handler executes on the form's thread.

    Do that by, in the form's event handler, checking 'this.InvokeRequired'. If it is true, use 'this.Invoke()' to transition to the form thread.

    See http://msdn2.microsoft.com/en-us/library/ms171728.aspx (but prefer to use BackgroundWorker, if you have the choice!)


  • Thread & ProgressBar