473,320 Members | 1,845 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 6018
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Dave | last post by:
I have 2 tables, one with names, and another with addresses, joined by their CIVICID number (unique to the ADDRESSINFO table) in Oracle. I need to update a field in the NAMEINFO table for a...
8
by: Lauren Quantrell | last post by:
In VBA, I constructed the following to update all records in tblmyTable with each records in tblmyTableTEMP having the same UniqueID: UPDATE tblMyTable RIGHT JOIN tblMyTableTEMP ON...
0
by: genojoe | last post by:
I am running an application that, when not used, just sits there firing a BackgroundWorker every 20 seconds. Every now and then, the BackgroundWorker freezes between the DoWork and...
3
by: nt8jbwu02 | last post by:
I am trying to use the example at: http://msdn2.microsoft.com/en-us/library/ms171728(d=ide).aspx to update a control in a thread safe maner. The article shows a couple of ways to do so and...
7
by: John J. Hughes II | last post by:
I have a DataGridView with a TextBoxColumn. I setting the data source to a List<stringvalue in a static class. The list is filled from a background thread. So far all is fine and it works...
8
by: =?Utf-8?B?R3JlZyBMYXJzZW4=?= | last post by:
I'm trying to figure out how to modify a panel (panel1) from a backgroundworker thread. But can't get the panel to show the new controls added by the backgroundwork task. Here is my code. In...
1
by: VAADADMIN | last post by:
I have a small app that I am working with to create LDIF files from text files. I have a pictureBox that has an animated GIF that will appear on the form when the LDIF are being created. The...
1
by: DrLargePants | last post by:
If I have something like below, and I catch an error in DoWork, how do I then get e.Error in RunWorkerCompleted to populate ? Private Sub bg_DoWork(ByVal sender As Object, ByVal e As...
2
by: melryin | last post by:
Hi, I'm a novice in C#. I wrote a program to export data from datagridview. While exporting, a pop-up window shows the progress of file exporting. The code is as follow: BackgroundWorker...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.