Solved

VB.Net Background Workers

Posted on 2013-11-04
3
206 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
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 85

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

12 Experts available now in Live!

Get 1:1 Help Now