473,699 Members | 2,506 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multithreading a databound app?

I am looking for a resource to learn how to do multithreading in a data
bound app. So far, I have figured out that I can't update a databound object
on a worker thread, because that triggers a cross-thread call to the UI
control that it is bound to.

I have created a simple demo to explore the problem. The demo has a
dataGridView control, a bindingSource control, a backgroundWorke r control
and a Go button. The grid is bound via the BindingSource to WidgetList,
which is a llist of WidgetItem objects derived from BindingList<T>.
WidgetList has one method, PopulateList(), which simply adds 100 WidgetItems
to itself, very slowly.

At run time, the WidgetList is instantiated and bound to the bindingSource
control in the FormLoad method. The backgroundWorke r's DoWork event handler
calls the WidgetList's PopulateList() method.

I had hoped I could populate the list on a worker thread without triggering
a UI control call. I'm getting a cross-thread UI call exception, even when I
suspend binding on the bindingSource and the dataGridView.

The other demos I have downloaded all seem to fudge the issue by creating an
object in the worker thread, then binding it after the thread completes.
That won't work for me, since my production app needs to transform the same
collection in a series of operations.

Can a databound object be updated on a worker thread? If so, how is it done?
Has anyone seen any good articles that discuss the issue? Thanks.

--
David Veeneman
Foresight Systems
Aug 1 '06 #1
6 5673
David,

No, it can't be done. You should probably unbind the object itself,
perform your operation, and then bind the object back.

Hop ethis helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"David Veeneman" <da****@nospam. com (domain is my last name)wrote in
message news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
>I am looking for a resource to learn how to do multithreading in a data
bound app. So far, I have figured out that I can't update a databound
object on a worker thread, because that triggers a cross-thread call to the
UI control that it is bound to.

I have created a simple demo to explore the problem. The demo has a
dataGridView control, a bindingSource control, a backgroundWorke r control
and a Go button. The grid is bound via the BindingSource to WidgetList,
which is a llist of WidgetItem objects derived from BindingList<T>.
WidgetList has one method, PopulateList(), which simply adds 100
WidgetItems to itself, very slowly.

At run time, the WidgetList is instantiated and bound to the bindingSource
control in the FormLoad method. The backgroundWorke r's DoWork event
handler calls the WidgetList's PopulateList() method.

I had hoped I could populate the list on a worker thread without
triggering a UI control call. I'm getting a cross-thread UI call
exception, even when I suspend binding on the bindingSource and the
dataGridView.

The other demos I have downloaded all seem to fudge the issue by creating
an object in the worker thread, then binding it after the thread
completes. That won't work for me, since my production app needs to
transform the same collection in a series of operations.

Can a databound object be updated on a worker thread? If so, how is it
done? Has anyone seen any good articles that discuss the issue? Thanks.

--
David Veeneman
Foresight Systems

Aug 1 '06 #2
See my post a few days ago; in the end I wrote a ThreadedBinding
(equiv. to Binding) to help with this issue; following link may work:

http://groups.google.com/group/micro...078656d6f1ee1f

Or: Tues, Jul 25 2006 9:31 am, by myself, titled "Threaded Bindings
don't update"

Hope it helps,

Marc

Aug 1 '06 #3
Thanks--but how do I unbind the object? I've tried setting the DataSource of
the bindingSource and the dataGridView to null, but I'm still getting the
same illegal cross-thread call exception on the DGV.

Thanks agsin for your help.

--
David Veeneman
Foresight Systems
Aug 1 '06 #4
I found my answer--see below. Thanks again for your help.
Aug 1 '06 #5
I found my answer. Thanks to those who responded to my original question!

As Nicholas Paladino pointed out, you have to disconnect a control from a
bound object before you update the object on a worker thread. Then update
it, and when the worker thread completes, rebind the resulting object.

In my demo project, I am using a dataGridView control bound to a
bindingSource control. The binding source control is bound to a WidgetList.
Here's how I fixed my code:

(1) I threw out the WidgetList member variable. Instead, I created it as a
local variable in the FormLoad event handler and set it as the
bindingSource.D ataSource.

private void this_Load(objec t sender, EventArgs e)
{
WidgetList widgetList = new WidgetList();
widgetListBindi ngSource.DataSo urce = widgetList;
}

(2) In the button handler that triggers the background worker, I get the
WidgetList from the bindinmgSource, then clear bindingSource.D ataSource. I
also show a label and progress bar to show the progress of the operation. I
pass the WidgetList to the worker thread as the argument to
RunWorkerAsync( ):

private void toolBarButtonGo _Click(object sender, EventArgs e)
{
statusStripLabe l1.Visible = true;
statusStripProg ressBar1.Visibl e = true;
WidgetList widgetList = (WidgetList)wid getListBindingS ource.DataSourc e;
widgetListBindi ngSource.DataSo urce = null;
backgroundWorke r1.RunWorkerAsy nc(widgetList);
}

(3) The backgroundWorke r.DoWork event fires. I get the WidgetList from the
event args and pass it to my worker method. The worker method returns the
loaded WidgetList, which I put into the event args:

private void backgroundWorke r1_DoWork(objec t sender, DoWorkEventArgs e)
{
// Call method to be run on worker thread
WidgetList argumentList = (WidgetList)e.A rgument;
WidgetList resultList = this.PopulateLi st(argumentList );
e.Result = resultList;
}

Here is the worker method from my demo:

private WidgetList PopulateList(Wi dgetList widgetList)
{
for (int i = 0; i < 100; i++)
{
// Add new widget to the list
WidgetItem widget = new WidgetItem(i);
widgetList.Add( widget);

// Pause to simulate slow process
Thread.Sleep(10 0);

// Report progress
backgroundWorke r1.ReportProgre ss(i);
}
return widgetList;
}

(4) When the worker thread completes, I get the WidgetList from the
backgroundWorke r.RunWorkerComp leted event args and re-bind the binding
source to it. I also hide the progress bar and label:

private void backgroundWorke r1_RunWorkerCom pleted(object sender,
RunWorkerComple tedEventArgs e)
{
WidgetList widgetList = (WidgetList)e.R esult;
widgetListBindi ngSource.DataSo urce = widgetList;
statusStripLabe l1.Visible = false;
statusStripProg ressBar1.Visibl e = false;
}

(5) While the operation is in progress, my worker method periodically
instructs the background worker to report its progress (see worker method
above).

(6) The backgroundWorke r.ProgressChang ed event fires each time
backgroundWorke rReportProgress () is called. It simply updates the progress
bar:

private void backgroundWorke r1_ProgressChan ged(object sender,
ProgressChanged EventArgs e)
{
statusStripProg ressBar1.Value = e.ProgressPerce ntage;
}

The overall result is that I have preserved the identity of my WidgetList
and updated it on a worker thread, without making any significant changes to
my data binding scheme.

--
David Veeneman
Foresight Systems


Aug 1 '06 #6
An update--I subsequently discovered that SuspendBinding( ) and
ResumeBinding() do eliminate the cross-thread control call problem in my
original message. You don't need to set the DataSource to null.

The trick is to call SuspendBinding( ) before calling
BackgroundWorke r.RunWorkerAsyn c(), and to call ResumeBinding() after the
asynchronous process completes-- in the RunWorkerComple ted() event handler
or later. Here is how I did the second step:

// Re-bind binding source control to updated widget list
widgetListBindi ngSource.DataSo urce = newWidgetList;
widgetListBindi ngSource.Resume Binding();

The newWidgetList object was returned to me in the
RunWorkerComple tedEventArgs.Re sult property:

WidgetList widgetList = (WidgetList)e.R esult;
--
David Veeneman
Foresight Systems

Aug 3 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

47
3732
by: mihai | last post by:
What does the standard say about those two? Is any assurance that the use of STL is thread safe? Have a nice day, Mihai.
16
8501
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
5
2135
by: sarge | last post by:
I would like to know how to perform simple multithreading. I had created a simple form to test out if I was multithreading properly, but got buggy results. Sometime the whole thig would lock up when I got two threads going at the same time. What I have is two text boxes (textBox1 and textBox2) and four buttons(cmdStartThread1, cmdStartThread2, cmdStopThread1, cmdStopThread2)
9
2459
by: tommy | last post by:
hi, i have found a example for multithreading and asp.net http://www.fawcette.com/vsm/2002_11/magazine/features/chester/ i want to speed up my website ... if my website is starting, they should build a database-connection and send a few sqls
2
2310
by: Rich | last post by:
Hello, I have set up a multithreading routine in a Test VB.net proj, and it appears to be working OK in debug mode and I am not using synchronization. Multithreading is a new thing for me, and I just wanted to ask if I am missing anything based on the following scenario. My test app pulls data from a large external data source which has a table-like structure (but not rdbms - more
55
3307
by: Sam | last post by:
Hi, I have a serious issue using multithreading. A sample application showing my issue can be downloaded here: http://graphicsxp.free.fr/WindowsApplication11.zip The problem is that I need to call operations on Controls from a delegate, otherwise it does not work. However each time I've done an operation, I must update the progressbar and progresslabel, but this cannot be done in the delegate as it does not work.
5
14107
by: clickon | last post by:
This is driving me nuts, it is such a simply thing to do but i cannot for the life of me work out how you are suposed to do it. I want to update the data in DropDownListB based on what is selected in DropDownListA. I am not trying to do anything fancy with AJAX, i am happy to use Post Backs. If it were two controls just on the page then i would simply specify a control parameter for the SelectCommand of the SQLDataSource control which...
5
2485
by: sandy82 | last post by:
Whats actuallly multithreading is ... and how threading and multithreading differ . Can any1 guide how multithreading is used on the Web .. i mean a practical scenario in which u use multithreading online using C# .
7
16307
by: Ray | last post by:
Hello, Greetings! I'm looking for a solid C++ multithreading book. Can you recommend one? I don't think I've seen a multithreading C++ book that everybody thinks is good (like Effective C++ or Exceptional C++, for example). Platform-specific (e.g.: Win32, POSIX) is OK, as long as it's good :) Thank you, Ray
0
8686
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9173
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8911
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7748
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5872
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4375
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3057
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2009
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.