namespace My_Samples { using System; using System.Threading; //Declare the call back delegate. public delegate void CallBackMethod(int returnValue); public class MyClass { //Add a local variable for the deleage. private CallBackMethod callBack; //Add a constructor with delegate type as parameter. public MyClass(CallBackMethod c) { callBack = c; } public void MyMethod(object data) { Console.WriteLine(data.ToString()); //The thread process is completed and now I am invoking the call back. if (callBack != null) callBack(100); } } class Program { //Declare the call back method public static void CallBackResult(int result) { Console.WriteLine("Call back method called {0}", result); } static void Main(string[] args) { //Pass the delegate as object MyClass obj = new MyClass(new CallBackMethod(CallBackResult)); ParameterizedThreadStart entryPoint = new ParameterizedThreadStart(obj.MyMethod); Thread thread = new Thread(entryPoint); thread.Start("Hello world"); Console.WriteLine("Main method is completed..."); Console.ReadLine(); } } }
How to receive data from thread? - Thread and call back methods.
How we can return some data from the thread? .net provides a call back method and you can call the call back method from the thread and send the data to the caller. Let's see a simple example.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment