Solved

VB.Net Background Workers

Posted on 2013-11-04
3
214 Views
Last Modified: 2013-11-25
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
0
Comment
Question by:matthewgreen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 18

Accepted Solution

by:
lludden earned 200 total points
ID: 39621957
If you want to actually update a control from another thread, you need to use the invoke method and a delegate.

What I do is have the bindingsource updated in the background thread (usually the slow part), then let the grid redraw itself,

If you are using datatables as a datasource, all the coding to implement it is done.  If you are using a custom class/collection, you need to implement INotifiyPropertyChanged in the class and IBindingList in the collection.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 39621977
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).
0
 

Author Comment

by:matthewgreen
ID: 39621983
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
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question