Updating a progressbar from a seperate thread
-
My program uses multiple threads to do something, and i want to make a progress bar to show the process status, the problem is that i can't update the progressbar from the thread (gives an exception..not the thread it was created on). what can i do?
-
My program uses multiple threads to do something, and i want to make a progress bar to show the process status, the problem is that i can't update the progressbar from the thread (gives an exception..not the thread it was created on). what can i do?
Use the standard technique (InvokeRequired/Invoke) explained over and over in CP articles; both of my articles contain a simple example. :)
Luc Pattyn [My Articles]
-
Use the standard technique (InvokeRequired/Invoke) explained over and over in CP articles; both of my articles contain a simple example. :)
Luc Pattyn [My Articles]
Uhh...well...I read a bit and it seems to be really confusing...a small simplified example would be nice :\
-
Uhh...well...I read a bit and it seems to be really confusing...a small simplified example would be nice :\
Hi, the last code snippet in my Sokoban article shows the simplest example possible: executing a method with one argument on the UI thread (it is an excerpt from file MainForm.cs) :)
Luc Pattyn [My Articles]
-
Hi, the last code snippet in my Sokoban article shows the simplest example possible: executing a method with one argument on the UI thread (it is an excerpt from file MainForm.cs) :)
Luc Pattyn [My Articles]
Uhh...i don't get it (especially cause I'm not very familiar with delegates...so can you explain what this does:
Invoke(new MovePaster(pasteOneMove), new object[]{c});
You send a function into a delegate? but the function is void and has a char as a parameter (And so does the delegate)...so...uhh...I'm confused :\ -
Uhh...i don't get it (especially cause I'm not very familiar with delegates...so can you explain what this does:
Invoke(new MovePaster(pasteOneMove), new object[]{c});
You send a function into a delegate? but the function is void and has a char as a parameter (And so does the delegate)...so...uhh...I'm confused :\Hi, whenever you create a Win app in .NET you use delegates; how else would you connect an event handler (e.g. myButton_Click) to the corresponding event ? When you write
myButton.Click+=new EVentHandler(myButton_Click);
you pass a method to the handler's constructor; this does not call myButton_Click, it tells the button that when clicked it will have to run myButton_Click for this object. This is fundamental to .NET, whether using C# or not. Invoke is similar, the line
Invoke(new MovePaster(pasteOneMove), new object[]{c});
will cause the execution of the specified method (with specified argument list), so it logically corresponds to a simple pasteOneMove(c); but it does not execute the method on the calling thread, it sends a message to the message pump and that message causes pasteOneMove(c); to be executed on the UI thread. A delegate is used gfor this; it is an object used to combine the relevant object and method in a structured (type-safe) way. I recommend you read more about this in any .NET programmers book, then have a look at some of the many examples around. :)
Luc Pattyn [My Articles]
-
Hi, whenever you create a Win app in .NET you use delegates; how else would you connect an event handler (e.g. myButton_Click) to the corresponding event ? When you write
myButton.Click+=new EVentHandler(myButton_Click);
you pass a method to the handler's constructor; this does not call myButton_Click, it tells the button that when clicked it will have to run myButton_Click for this object. This is fundamental to .NET, whether using C# or not. Invoke is similar, the line
Invoke(new MovePaster(pasteOneMove), new object[]{c});
will cause the execution of the specified method (with specified argument list), so it logically corresponds to a simple pasteOneMove(c); but it does not execute the method on the calling thread, it sends a message to the message pump and that message causes pasteOneMove(c); to be executed on the UI thread. A delegate is used gfor this; it is an object used to combine the relevant object and method in a structured (type-safe) way. I recommend you read more about this in any .NET programmers book, then have a look at some of the many examples around. :)
Luc Pattyn [My Articles]
So if i want to update my progress bar...i need to do something like this?
private delegate void SetPrecentage(int p);
private int DoOperation(int p) { if (this.InvokeRequired){ Invoke(new SetPrecentage(DoOperation),new object[]{p});} }
but then...how do i like, update the progress bar? -
So if i want to update my progress bar...i need to do something like this?
private delegate void SetPrecentage(int p);
private int DoOperation(int p) { if (this.InvokeRequired){ Invoke(new SetPrecentage(DoOperation),new object[]{p});} }
but then...how do i like, update the progress bar?Do that in the else block.
private int DoOperation(int p)
{
if (this.InvokeRequired)
{
Invoke(new SetPrecentage(DoOperation),new object[]{p});}
}
else
{
// The else block runs on the UI thread, so can update the progressbar safely.
}Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro
-
Do that in the else block.
private int DoOperation(int p)
{
if (this.InvokeRequired)
{
Invoke(new SetPrecentage(DoOperation),new object[]{p});}
}
else
{
// The else block runs on the UI thread, so can update the progressbar safely.
}Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro
but i mean, it will never run on a UI thread, so how do i update the progressbar? what will invoking p do? :\
-
but i mean, it will never run on a UI thread, so how do i update the progressbar? what will invoking p do? :\
The else code runs on the UI thread. When you call BeginInvoke or Invoke and pass it a delegate, .NET calls the delegate on the UI thread. In your example, you passed the same function as the delegate. When .NET calls the function on the UI thread,
InvokeRequired
will be false and the else code will run on the UI thread. Have a look at What's up with BeginInvoke[^] to know more about this.Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro