Solved

how make callback method in C#

Posted on 2006-07-24
6
446 Views
Last Modified: 2010-04-16
Hi expert

Currently I am developing one application
I want to call user thread from worker thread.
for that i need callback function for UI updation.
how i can i do this .

Thanks in advance
0
Comment
Question by:avinash_takale
6 Comments
 
LVL 9

Accepted Solution

by:
Thalox earned 125 total points
ID: 17166534

you can do this by using delegates.

have a look at

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDelegateClassTopic.asp

or simply type delegate in your visual studio and press F1

hope that helps,

Thalox
0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 125 total points
ID: 17166784
0
 

Expert Comment

by:muskok
ID: 17167400
1) define a delegate with the same signature as your method that will update UI
2) In worker thread create a new delegate of this type
3) use Control.Invoke to invoke the delegate in Main Thread

Note: The thread method must reach at least one member control (may be a form) to invoke the method.

Sample:
//Assume we have a text box named "m_txtMessage"

//Delegate that will be used to call the method
public delegate void MyUpdaterHandler(string newText);

private void UpdateUI(string newText)
{
  m_txtMessage.Text = newText;
}

//Your worker thread method
private void WorkerThreadMethod()
{
  //Do something in different thread
  //.....
 
  //Create a new delegate
  MyUpdaterHandler  _dlg = new MyUpdaterHandler(this.UpdateUI);

  //Invoke delegate in the thread whic created the control   txtMessage (which is Main Thread) you can also use main form instance
 
 txtMessage.Invoke(dlg, new object[]{ "Text updated in Main Thread" });
}

//UpdateUI()  method will be called on the thread that created the control "txtMessage"
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

932 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

9 Experts available now in Live!

Get 1:1 Help Now