backgroundworker button updates

  • Thread starter Thread starter auldh
  • Start date Start date
A

auldh

hello,
as i work through upgrading and converting some old VB application to C#.
i'm finding that i need to add threading. i'm using backgroundworker as the
method to do so.

i'm running into and issue where i was attempting to do windows form button
updates like enable, disable and text.

i'm using multiple class.cs files to make the code better to manage but i
ran into issues where i can get the code to update the buttons states and
text but they don't appear to display the changes.

and it became a nightmare to debug the project would hit the breakpoint and
go dead. anyway the problem i want to get help on is this:
what is the best method/process to manage button state changes and text
updates while using "backgroundworker"? how can i get them to do .refresh()
like?

do i use the "progresschanged" or "invoke" method.

and any good to great reference books, authors to lean about threading and
backgroundworker threading?

thanks in advance for any help.
 
For whatever that can be change after/before the threaded job it is best to
that from the appropriate UI handler (or do you really want to change button
states from the background task ?).

For whatever you change while the job is running, do that from
ProgressChanged.

The basic idea is that :
- you can only update the UI from the UI thread
- the backgroundworker class calls each of its event on the appropriate
thread (i.e. runs the actual work on a background thread, report progress
and completion on the UI thread).

Finally keep in mind that the less you update the ui the better it is (else
you end up with keeping the ui busy while freeing the ui thread is the
reason for which you are using a background thread in the first place).

__
Patrice
 
do i use the "progresschanged" or "invoke" method.

and any good to great reference books, authors to lean about threading and
backgroundworker threading?

You can use the invoke method. I generally create a small helper
method like the following that any thread (including the GUI thread
itself) can call to update the state of UI elements:

delegate void UpdateProgressDelegate(int cur, int max);
public void UpdateProgress(int cur, int max)
{
if (InvokeRequired)
{
Invoke(new UpdateProgressDelegate(UpdateProgress),
new object[] { cur, max });
return;
}

progressBar1.Maximum = max;
progressBar1.Value = cur;
}
 
auldh said:
[...] anyway the problem i want to get help on is this:
what is the best method/process to manage button state changes and text
updates while using "backgroundworker"? how can i get them to do .refresh()
like?

do i use the "progresschanged" or "invoke" method.

You can use either. But, as Patrice suggests, if you are using
BackgroundWorker, it makes MUCH more sense to use the ProgressChanged
event, because that's the whole point of using BackgroundWorker. If
you're going to use the Control.Invoke() method, you might as well do
that for everything and use the thread pool or explicit thread for the
background task.
and any good to great reference books, authors to lean about threading and
backgroundworker threading?

MSDN has plenty of documentation for the BackgroundWorker class, and
this newsgroup – which you can review and search past articles using
Google Groups – has quite a lot of discussion on the topic.

Pete
 
Random,
i tried that but the updates did not display on the UI.
the UI buttons states did not change and i could not find away to refresh
the screen in realtime. that was one reason i had to gut all the thread code
out. i spent hours on trying to troubleshoot the button updates. the text
update to a textbox was almost bullet proof it was just the button state
changes from enable to disable and
..text = "running..." never really happen. and don't know why.



Random said:
do i use the "progresschanged" or "invoke" method.

and any good to great reference books, authors to lean about threading and
backgroundworker threading?

You can use the invoke method. I generally create a small helper
method like the following that any thread (including the GUI thread
itself) can call to update the state of UI elements:

delegate void UpdateProgressDelegate(int cur, int max);
public void UpdateProgress(int cur, int max)
{
if (InvokeRequired)
{
Invoke(new UpdateProgressDelegate(UpdateProgress),
new object[] { cur, max });
return;
}

progressBar1.Maximum = max;
progressBar1.Value = cur;
}

.
 
auldh said:
Random,
i tried that but the updates did not display on the UI.
the UI buttons states did not change and i could not find away to refresh
the screen in realtime. that was one reason i had to gut all the thread code
out. i spent hours on trying to troubleshoot the button updates. the text
update to a textbox was almost bullet proof it was just the button state
changes from enable to disable and
..text = "running..." never really happen. and don't know why.

If you don't post the code, no one else can tell you why either.

Control.Invoke() would work for an otherwise correct program. For a
program that's not otherwise correct, all bets are off. But we can't
offer advice for code we haven't even seen.

Pete
 
i tried that but the updates did not display on the UI.
the UI buttons states did not change and i could not find away to refresh
the screen in realtime. that was one reason i had to gut all the thread code
out. i spent hours on trying to troubleshoot the button updates. the text
update to a textbox was almost bullet proof it was just the button state
changes from enable to disable and
.text = "running..."  never really happen. and don't know why.

The code I posted was meant to be used with a thread.

Guessing, but it sounds like you attempted to update the UI from the
UI thread while continuing to do work in the UI thread. This isn't
generally a good idea, as it'll block the updates (as you've seen) and
otherwise make your app unresponsive.

If you have a lot of work to do, either find ways to break it it up
into smaller peices that can be run from a timer or something
similiar, or spin the work off into a seperate thread.
 
Back
Top