Solved

VB.Net Background Workers

Posted on 2013-11-04
3
205 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
3 Comments
 
LVL 18

Accepted Solution

by:
lludden earned 200 total points
Comment Utility
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 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now