Update GUI from RunWorkerCompleted

W

wilkokosten

Dear all,

I have the following question:

I want to populate a datagridview after my BackGroundWorker has finished. I
thought i could use the RunWorkerCompleted event to update my datagridview
but this is not working 'always'.

The strange thing is that when i use a bindingsource directly on the
datagridview a crossthread exception is thrown. But when i am using a typed
dataset it is going ok. I supposed both are created on the GUI thread, thats
why i want to use the RunWorkerCompleted event to update the gui after the
workerthread is finished.

Anybody suggestions?
Should i use the RunWorkerCompleted event for this purpose?

Kind Regards,

Wilko
 
M

Marc Gravell

Hmmm... IIRC RunWorkerCompleted should fire on the UI thread... but I guess
you could use belt'n'braces and simply use Control.Invoke in the
event-handler...

this.Invoke((MethodInvoker) delegate {
// your real code
});

Are you sure you aren't also updating the list/table directly in the DoWork
event?

Marc
 
W

wilkokosten

Hi Peter and Marc,

Thanks for the reply, the problem i have has to do with the way i
implemented the backgroundworker. If i implement the backgroundworker on a
normal way it is going ok, i have to find out why it is going wrong in my
custom worker.

Kind regards,

Wilko




Peter Duniho said:
[...]
The strange thing is that when i use a bindingsource directly on the
datagridview a crossthread exception is thrown. But when i am using a
typed
dataset it is going ok. I supposed both are created on the GUI thread,
thats
why i want to use the RunWorkerCompleted event to update the gui after
the
workerthread is finished.

Anybody suggestions?
Should i use the RunWorkerCompleted event for this purpose?

As Marc said, the RunWorkerCompleted event should be raised on the GUI
thread, just as you expected it to.

I'll respectfully disagree with his recommendation to use Invoke(). The
fact is, RunWorkerCompleted _should_ work. If it's not working, that
means something else is going wrong that's not understood. It would be
one thing if you knew what was going wrong and decided to use Invoke() to
fix it. But until you know what's going wrong, it's inappropriate to hack
in something that seems to make the problem go away.

As far as your specific problem goes: if you're getting a cross-thread
exception from code that's actually being executed from the
RunWorkerCompleted event, that strongly suggets that the UI object you're
using somehow got created on a different thread, or that the
BackgroundWorker was for some reason not created on the UI thread.

Without a concise-but-complete code sample that reliably demonstrates the
problem, it's hard to say specifically what might be going wrong. But the
fact is, if you're getting that exception, there's some mistake somewhere
in how these things are being set up. You should find that mistake. If
at that point you decide to work around it instead of fixing it, that's
"fine". But don't work around a mistake until you know what the mistake
is and can make an informed decision about what the right fix is.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top