Solved

how make callback method in C#

Posted on 2006-07-24
6
445 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

20 Experts available now in Live!

Get 1:1 Help Now