Link to home
Start Free TrialLog in
Avatar of Steve Marshall
Steve MarshallFlag for United Kingdom of Great Britain and Northern Ireland

asked on

VB.Net Background Workers

Hi all,

A (hopefully) quick question. I have a VB.Net application linked to MS Access databases where the main form has 3 Data Grid Views presenting different cuts across scheduled manufacturing processes - one for "Pending" jobs, one for "Planned" jobs, and one showing a Machine Loading Summary for the next 10 working days.

I would like to use Background Workers to control the refresh of the data in these grids to provide better "performance" in the screen refreshes, because although not slow per se, I believe that the refreshing of the grids could be made to appear a lot faster using BGWs because data refreshes for the the three could in effect be done in parallel.

Anyway, I am clearly missing something because I keep getting a message about being "cross-threaded" or some such. I know that I cannot directly affect the "user interface" from the BGW, and that this must be done from the thread that instantiated the elements. I had a very simple version of BGW use working (based on something I found on the web), but this does not work here so that's why I think something is missing!

In simple terms what I want to do is populate my grids from a data set initially, and then when a refresh becomes due the BGW is activated, it generates an updated set of data, and when complete it notifies the main thread which then loads that data to the grid on screen and carries out the associated formatting (meaning that all manipulation of the UI is in the main thread).

Anyone able to in a few lines summarise what should be done where in the BGW events to achieve this, and what I need to add to the main thread ?? I am assuming that after the mainthread has called BGW.RunWorkerAsynch(), the population of the data is carried out in the BGW_DoWork() event, and when complete the BGW_RunWorkedCompleted()  fires (I do not use the BGW_ProgressChanged() event as yet as I cannot see a reason for it at this point). Is it the RunWorkerCompleted() that actually refreshes the grid, or does this pass control to something else?? I think what I am asking (in a long winded way!!) is - "Is the RunWorkerCompleted() event part of the main thread or the background thread??"

Thanks,
Matt
ASKER CERTIFIED SOLUTION
Avatar of lludden
lludden
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Is the RunWorkerCompleted() event part of the main thread or the background thread??

The RunWorkerCompleted() event fires in the main UI thread.  It is safe to update the UI from there.  

If you're getting a cross-thread error it is occurring from somewhere else in the code (probably in the DoWork() handler).
Avatar of Steve Marshall

ASKER

Ah, I will read up on Invoke and delegates - not used these before. I am using DataTables as you say, so hopefully there will not be much reading or code changing.

I will search to the web to find an example on this

Thanks,
Matt