473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

difference is between using ThreadStart and delegates instead ofusing BeginInvoke


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.
Nov 30 '07 #1
1 2677
A few differences... ThreadStart gives you full control of the thread
(rather than the delegate which will probably use a pool-thread [which
is often fine]). Actually, a third comparison might be to
ThreadPool.QueueUserWorkItem.
Also, the callback mechanism is different: with ThreadStart you do it
yourself; with BeginInvoke there is a common pattern (which personally
I don't find very intuitive). But note that this pattern is also often
used with completion port semantics, which abstracts whether you are
waiting on an actual thread, versus IO.

As a fourth thing - watch out for Control.BeginInvoke - the meaning is
different to Delegate.BeginInvoke.

Marc
Nov 30 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: theBoringCoder | last post by:
Hello All, I just started learning about Delegates, and was wondering if someone could point me at some useful, straightforward examples of how to use delegates to make method calls...
12
by: VM | last post by:
Hi, In my Win app, I'm using delegates because the process that'll be running is time-consuming and it requires parameters. The only problem I'm having is that the lines following the BeginInvoke...
11
by: Doug Thews | last post by:
I've been working on some samples that use BeginInvoke/EndInvoke. In one example, I call BeginInvoke and pass it an AsyncCallback function pointer. I was messing around with ReaderWriterLocks and...
2
by: cmrchs | last post by:
Hi, how can I call a method, being referenced by a delegate object, via a thread ? Here's what I try : public delegate void PrintDelegate(); static void Print() {...
7
by: Waleed AlRashoud | last post by:
Hi All, I hvae worker class to manage the application inside worker : worker creates (n) number of threads to do IO operations. The Problem Is: to handle all threads : , , I have to...
15
by: Matt | last post by:
Hi There, Can anyone explain me the real advantages of (other than syntax) lambda expressions over anonymous delegates? advantage for one over the other. delegate int F(int a); F fLambda = a...
14
by: Gotch | last post by:
Hi all, I've recently digged into C# and the whole .Net stuff. Particularly I found the idea of adding Events and Delegates to the C# language very interesting and I'm trying to use them in...
15
by: Pixel.to.life | last post by:
Dear All, Here is a problem I am facing (it might be too simple, but then I admit I am not a Guru:-) I have a main thread, in managed C++, that deals with displaying a form and some controls....
1
by: puzzlecracker | last post by:
Below is the example that I mercilessly copied from Jon's article. In this example, I do not understand how his AsyncResult was able to retrieve delegates. In other words, using AsyncResult...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.