471,338 Members | 1,250 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

Update GUI from RunWorkerCompleted

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

Mar 28 '08 #1
3 5917
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
Mar 28 '08 #2
On Fri, 28 Mar 2008 05:09:01 -0700, wilkokosten
<wi*********@discussions.microsoft.comwrote:
[...]
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
Mar 28 '08 #3
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" wrote:
On Fri, 28 Mar 2008 05:09:01 -0700, wilkokosten
<wi*********@discussions.microsoft.comwrote:
[...]
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
Mar 31 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Dave | last post: by
8 posts views Thread by Lauren Quantrell | last post: by
8 posts views Thread by =?Utf-8?B?R3JlZyBMYXJzZW4=?= | last post: by
1 post views Thread by DrLargePants | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.