delegate question!

  • Thread starter Thread starter DBC User
  • Start date Start date
D

DBC User

Hi all,

Could someone explain me the following

I have a winform. I call a long process in async model with a callback.
Now that once I get the call back I update a message to the main
winform. Well I got everything to work (based on an example), but I
didn't understand the fullflow. Why do I need to call a delegate to
update the main winform?


//Delegate declaration
delegate void CallDelegate(XmlNode xNode);

//Display information back to main thread
private void CallThread(XmlNode xNode)
{
if (this.Grid1.InvokeRequired == false)
{
DataSet resultDS=null;
DataSet ds = GetData();
ProcessDataandShow();
DisplayComplete();
}
else
{
CallDelegate calMain = new CallDelegate(CallThread);
BeginInvoke(calMain, new object[]{xNode});
}
}


//Callback for long process on completion
private void GetProcComplete(object sender, EventArgs
statusInformation)
{
CallMainThread(statusInformation.XNode);
}

I appriciate if someone can educate me on this?
Thanks in advance.
DBC
 
DBC,

When updating a user interface, any changes to the interface have to be
made from calls on the UI thread. In order to do this, the Control class
offers a method, Invoke, which will take any delegate and an array of
objects to pass to the delegate, and call it on the UI thread. The reason a
delegate is used is because it is an easy way to represent a method in a
generic manner.

Hope this helps.
 
I understood all the way upto Invoke part. Forgive my ignorance, but I
couldn't understand the delegate part. Here is my question;

My long process runs in a seperate thread. On the completion, the
complete program also stays in the same thread. Here in the worker
thread, I am creating a delegate, which doesn't have any connection to
the main thread( or does it???). How does the begin invoke on the
worker thread connects to the main thread?????

Thank you for the patience.
DBC
 
Have a look at Ted Pattison's articles on threads and the UI in MSDN


http://msdn.microsoft.com/msdnmag/find/default.aspx?type=Au&phrase=Ted Pattison

June 2004 talks about the Updating the UI from secondary threads and
the ideas around it.

It is in VB.Net rather than c# but he explains the concepts a lot
better than I could.

He also covers other threading related topics in other artices.
- how to use delegates for async execution
- managing threads
- syncing threads.

hth,
Alan.
 
It has good article I am going to go through them.
Quick question though, in that article, he mention the following;

"Here's where things get interesting. When the secondary thread calls
BeginInvoke, Windows Forms automatically calls the UpdateUI_Impl method
on the primary UI thread".

Does it mean, anytime when a delegate is called, regardless of which
worker thread they are in, it get executed in the primary UI thread? or
is it my stupid assumption?

Thanks in advance.
 
DBC User,

No, the delegate gets executed on the thread it is called on when you
call it.

When you pass the delegate to the Invoke/BeginInvoke method, it sends a
message to the message loop for the window which is processed on the UI
thread. This message has a parameter associated with it that points to the
delegate and object array that you passed to invoke (more or less), and then
invokes it on the UI thread.
 
It has good article I am going to go through them.
Quick question though, in that article, he mention the following;

"Here's where things get interesting. When the secondary thread calls
BeginInvoke, Windows Forms automatically calls the UpdateUI_Impl method
on the primary UI thread".

Does it mean, anytime when a delegate is called, regardless of which
worker thread they are in, it get executed in the primary UI thread? or
is it my stupid assumption?

No. The key is that you must call the Invoke or BeginInvoke methods on the
*control*, not on the delegate itself. You pass in the delegate you wish to
be executed as a parameter. The control knows which thread should execute
the delegate.
 
Ok, I think I am getting this now. So In my example, when I called
BeginInvoke, it is getting executed on the control (in this case
mainForm). So If I want the delegate to execute on a datagrid
control(for ex datagrid1). I say datagrid1.BeginInvoke(...)
Is that correct?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top