473,326 Members | 2,196 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,326 software developers and data experts.

Passing Delegate into a function

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,

Nov 16 '05 #1
5 2323
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]
- mv*@spam.guard.caspershouse.com

"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:1C**********************************@microsof t.com...
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,

Nov 16 '05 #2
Mike <Mi**@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
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(),ThreadPriorit y.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 [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:1C**********************************@microsof t.com...
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,


Nov 16 '05 #4
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,tr ue);

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(),ThreadPriorit y.Normal,true);

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

Nov 16 '05 #5
Mike <Mi**@discussions.microsoft.com> wrote:
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(),ThreadPriorit y.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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

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

Similar topics

7
by: Richard Grant | last post by:
Hi. In c/C++ i can pass the address of a subroutine to another subroutine as an actual parameter How do I do that in VB .NET What should be the syntax for a parameter to receive the address of a...
6
by: Max | last post by:
Last time I tried to explain this on another forum it didn't go too well, so I'll try my best and if you know what I'm talking about then please tell me how to do this. I have a class, inside I...
0
by: Eric Sabine | last post by:
OK, I'm trying to further my understanding of threading. The code below I wrote as kind of a primer to myself and maybe a template that I could use in the future. What I tried to do was pass data...
3
by: Sam Learner | last post by:
Hello everyone, I am developping an application, I create a thread for the application because it is about to download a large file, and wanted it to do it inside of a thread... Now, the function...
10
by: ChrisB | last post by:
Coming from a C/C++ background, how would I pass a function pointer to a function? I want to write a function that handles certain thread spawning. Here's what I'm trying to invision: function(...
3
by: Lonewolf | last post by:
Hi all, I'm having difficulties passing data back to managed class from my native class when the data is generated from within a native thread in the native class itself. I will give the following...
0
by: Haxan | last post by:
Hi, I have an unmanaged application that converts a function pointer to a delegate and then pass this as a parameter(delegate) to a managed function which then invokes it. Currently Im able to...
12
by: Haxan | last post by:
Hi, I have my main application class(unmanaged) that has a none static member function that I need to pass as a delegate to managed C# method. In one of the methods of this class(unmamanged),...
2
by: darthghandi | last post by:
I am trying to pass a socket object when an event is signaled. I have successfully bound to a network interface and listened on a port for incoming connection. I have also been able to accept...
10
by: vcquestions | last post by:
Hi. Is there way to have a function pointer to a delegate in c++/cli that would allow me to pass delegates with the same signatures as parameters to a method? I'm working with managed code. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.