R
robparr
Hello, hopefully someone can help me.
I am reading and learning steadily about threading and asynchronous
programming, which to me sound like the same thing. At the moment I am
not sure what the difference is between using ThreadStart and
delegates instead of using BeginInvoke. Can both of them be used to
achieve the same thing? Or perhaps you should use one over the other?
I am slightly confused because I was looking at this example from mono
project (http://www.mono-project.com/ThreadsBeginnersGuide)
using System;
using System.Threading;
namespace ThreadGuideSamples {
public delegate void MyCallBack (int Response);
class ThreadCaller {
static void Main () {
ThreadCaller myCaller = new ThreadCaller ();
}
public ThreadCaller () {
// Passing an MyCallBack-Delegate to the object executed on the
// different thread.
SecondThreadObject myThreadObject = new
SecondThreadObject (new MyCallBack(this.OnResponse));
// Starting the thread as usual
Thread myThread = new Thread (new
ThreadStart (myThreadObject.MyAsyncMethod));
myThread.Start ();
}
// Target-Destination for messages from our thread
protected void OnResponse (int response) {
Console.WriteLine ("Value returned to " +
"main thread {0}", response);
}
}
class SecondThreadObject {
protected MyCallBack On_Response = null;
public SecondThreadObject (MyCallBack callback) {
this.On_Response = callback;
}
public void MyAsyncMethod () {
for (int i = 0; i < 50; i++)
if (this.On_Response != null)
// Invoke the call back delegate
this.On_Response (i);
Console.ReadKey ();
}
}
}
I wasn't quite sure why BeginInvoke, EndInvoke and AsyncCallback
delegate werent used.
I am reading and learning steadily about threading and asynchronous
programming, which to me sound like the same thing. At the moment I am
not sure what the difference is between using ThreadStart and
delegates instead of using BeginInvoke. Can both of them be used to
achieve the same thing? Or perhaps you should use one over the other?
I am slightly confused because I was looking at this example from mono
project (http://www.mono-project.com/ThreadsBeginnersGuide)
using System;
using System.Threading;
namespace ThreadGuideSamples {
public delegate void MyCallBack (int Response);
class ThreadCaller {
static void Main () {
ThreadCaller myCaller = new ThreadCaller ();
}
public ThreadCaller () {
// Passing an MyCallBack-Delegate to the object executed on the
// different thread.
SecondThreadObject myThreadObject = new
SecondThreadObject (new MyCallBack(this.OnResponse));
// Starting the thread as usual
Thread myThread = new Thread (new
ThreadStart (myThreadObject.MyAsyncMethod));
myThread.Start ();
}
// Target-Destination for messages from our thread
protected void OnResponse (int response) {
Console.WriteLine ("Value returned to " +
"main thread {0}", response);
}
}
class SecondThreadObject {
protected MyCallBack On_Response = null;
public SecondThreadObject (MyCallBack callback) {
this.On_Response = callback;
}
public void MyAsyncMethod () {
for (int i = 0; i < 50; i++)
if (this.On_Response != null)
// Invoke the call back delegate
this.On_Response (i);
Console.ReadKey ();
}
}
}
I wasn't quite sure why BeginInvoke, EndInvoke and AsyncCallback
delegate werent used.