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