Passing Delegate into a function

G

Guest

Hi! I am trying to write a function that will span a thread for me.
Eventually I want to incorporate this finction into a class:

public void SpanThread(string threadName,string threadDelegate
,ThreadPriority threadPriority, bool haveCallingThreadWaitToComplete)
{
// Bombs on this line:
ThreadStart newThreadStart = new ThreadStart(threadDelegate);

Thread newThread = new Thread(newThreadStart);
newThread.Name = threadName;
newThread.Priority = threadPriority;
newThread.Start();
if(haveCallingThreadWaitToComplete)
{
newThread.Join();
}
}

Question:

How do I pass the name of the delegate to this function so that the compiler
does not complain? Is it possible?

Thank you in advance,
 
N

Nicholas Paldino [.NET/C# MVP]

Mike,

You can not pass a string. Rather, you should declare threadDelegate as
type ThreadStart, and pass it in through there. If you really wanted to
take a string, then you would have to pass an object instance as well (or
not if the methods were static), and then use reflection to hook up the
delegate with the method info. Is that what you really want to do?

Hope this helps.
 
J

Jon Skeet [C# MVP]

Mike said:
Hi! I am trying to write a function that will span a thread for me.
Eventually I want to incorporate this finction into a class:

public void SpanThread(string threadName,string threadDelegate
,ThreadPriority threadPriority, bool haveCallingThreadWaitToComplete)
{
// Bombs on this line:
ThreadStart newThreadStart = new ThreadStart(threadDelegate);

Thread newThread = new Thread(newThreadStart);
newThread.Name = threadName;
newThread.Priority = threadPriority;
newThread.Start();
if(haveCallingThreadWaitToComplete)
{
newThread.Join();
}
}

Question:

How do I pass the name of the delegate to this function so that the compiler
does not complain? Is it possible?

You'd need to use reflection to get the appropriate MethodInfo for the
method you want to invoke, and then use Delegate.CreateDelegate to
create the appropriate delegate instance.
 
G

Guest

Thank you Nikolas. I changed the function signature as follows:
public static void SpanThread(string threadName,ThreadStart threadDelegate
,ThreadPriority threadPriority, bool haveCallingThreadWaitToComplete)

Then, I am trying to call it
SpanThread("someName",First_Thread(),ThreadPriority.Normal,true);

the compiler complains that it can not convert void to ThreadStart since
First_Thread() is a void type.

Any ideas what I am doing wrong ?

Many Thanks,

Nicholas Paldino said:
Mike,

You can not pass a string. Rather, you should declare threadDelegate as
type ThreadStart, and pass it in through there. If you really wanted to
take a string, then you would have to pass an object instance as well (or
not if the methods were static), and then use reflection to hook up the
delegate with the method info. Is that what you really want to do?

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Mike said:
Hi! I am trying to write a function that will span a thread for me.
Eventually I want to incorporate this finction into a class:

public void SpanThread(string threadName,string threadDelegate
,ThreadPriority threadPriority, bool haveCallingThreadWaitToComplete)
{
// Bombs on this line:
ThreadStart newThreadStart = new ThreadStart(threadDelegate);

Thread newThread = new Thread(newThreadStart);
newThread.Name = threadName;
newThread.Priority = threadPriority;
newThread.Start();
if(haveCallingThreadWaitToComplete)
{
newThread.Join();
}
}

Question:

How do I pass the name of the delegate to this function so that the
compiler
does not complain? Is it possible?

Thank you in advance,
 
R

Richard Blewett [DevelopMentor]

Its the First_Thread its complaining about , you should be giving it a function name not invoking First_Thread which is what the () is doing (which returns the void it is complaining about). Also the method is expecting an object of type ThreadStart not just a method name, Change your call to this

SpanThread("someName",new ThreadStart(First_Thread),ThreadPriority.Normal,true);

You wrap the method in the delegate instance and hand the delegate instance to SpanThread. Now when SpanThread wants to call the method it talks to the delegate and the delegate talks to the actual First_Thread method. In this way you have an extra level of indirection that hugely increases flexibility and decreases coupling between components.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

Thank you Nikolas. I changed the function signature as follows:
public static void SpanThread(string threadName,ThreadStart threadDelegate
,ThreadPriority threadPriority, bool haveCallingThreadWaitToComplete)

Then, I am trying to call it
SpanThread("someName",First_Thread(),ThreadPriority.Normal,true);

the compiler complains that it can not convert void to ThreadStart since
First_Thread() is a void type.
 
J

Jon Skeet [C# MVP]

Mike said:
Thank you Nikolas. I changed the function signature as follows:
public static void SpanThread(string threadName,ThreadStart threadDelegate
,ThreadPriority threadPriority, bool haveCallingThreadWaitToComplete)

Then, I am trying to call it
SpanThread("someName",First_Thread(),ThreadPriority.Normal,true);

the compiler complains that it can not convert void to ThreadStart since
First_Thread() is a void type.

Any ideas what I am doing wrong ?

Yes - you need to change the call to:

SpanThread ("someName", new ThreadStart(First_Thread),
ThreadPriority.Normal, true);

Currently, you're asking First_Thread to be called and the return value
(which it doesn't have) to be used as the parameter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top