Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how make callback method in C#

Posted on 2006-07-24
6
Medium Priority
?
459 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 500 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 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

636 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