Solved

how make callback method in C#

Posted on 2006-07-24
6
450 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

739 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