By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,118 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,118 IT Pros & Developers. It's quick & easy.

Function as parameter

P: n/a
I am looking for a way to pass a function as parameter, NOT A
DELEGATE.

what i am trying to do is a worker process as for example of what i
want to do :

public static void StartWorking(FUNCTION MyFunction)
{
//thread object
private Thread tWorkingProcess;

Set the thread to use the function
tWorkingProcess = new Thread(new ThreadStart(MyFunction));

//start the thread
tWorkingProcess.Start();

Etc...........
}

So with that kind of function i'll just need to pass the function i
want to run
in a thread to that function and it will run it. Delegate dont work
because
they are attached to 1 function and i would be stupid to create 1
delegate
for each function i have.

Take note that thread object take only VOID function, so i would need
to pass only void
to it.
Sep 16 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Sep 16, 2:01*pm, Franck <the_darkbl...@hotmail.comwrote:
I am looking for a way to pass a function as parameter, NOT A
DELEGATE.
I suspect you may be confused as to what a delegate is. I think it's
what you're looking for. In particular, the ThreadStart delegate type.
what i am trying to do is a worker process as for example of what i
want to do :

* * * * public static void StartWorking(FUNCTION MyFunction)
* * * * {
* * * * * * //thread object
* * * * * * private Thread tWorkingProcess;

* * * * * * Set the thread to use the function
* * * * * * tWorkingProcess = new Thread(new ThreadStart(MyFunction));

* * * * * *//start the thread
* * * * * * tWorkingProcess.Start();

* * * * * *Etc...........
* * * }

So with that kind of function i'll just need to pass the function i
want to run in a thread to that function and it will run it. Delegate dont work
because they are attached to 1 function and i would be stupid to create 1
delegate for each function i have.
It's unclear to me whether you're talking about delegate *instances*
or delegate *types*.
Take note that thread object take only VOID function, so i would need
to pass only void to it.
So you need ThreadStart, as I thought:

public static void StartWorking(ThreadStart function)
{
Thread tWorkingProcess = new Thread(function);
tWorkingProcess.Start();
}

Then:

private void Foo()
{
...
}

private void Bar()
{
...
}

// Somewhere else
StartWorking(Foo);
StartWorking(Bar);

What do you want to do that that isn't doing for you?

See http://pobox.com/~skeet/csharp/events.html for more information
about delegates.

Jon
Sep 16 '08 #2

P: n/a
On Sep 16, 8:01 am, Franck <the_darkbl...@hotmail.comwrote:
I am looking for a way to pass a function as parameter, NOT A
DELEGATE.

what i am trying to do is a worker process as for example of what i
want to do :

public static void StartWorking(FUNCTION MyFunction)
{
//thread object
private Thread tWorkingProcess;

Set the thread to use the function
tWorkingProcess = new Thread(new ThreadStart(MyFunction));

//start the thread
tWorkingProcess.Start();

Etc...........
}

So with that kind of function i'll just need to pass the function i
want to run
in a thread to that function and it will run it. Delegate dont work
because
they are attached to 1 function and i would be stupid to create 1
delegate
for each function i have.

Take note that thread object take only VOID function, so i would need
to pass only void
to it.

I'm not sure why you think a delegate is only attached to a single
function. This code works for me:

using System;
using System.Threading;

namespace ConsoleApplication1 {

public delegate void MyDelegate();

class Program {
static void Main(string[] args) {
StartWorking(function1);
StartWorking(function2);
StartWorking(function3);
Console.ReadLine();
}

public static void StartWorking(MyDelegate MyFunction) {
//thread object
Thread tWorkingProcess;

//Set the thread to use the function
tWorkingProcess = new Thread(new ThreadStart(MyFunction));

//start the thread
tWorkingProcess.Start();
}

private static void function1() {
Console.WriteLine("Function 1");
}

private static void function2() {
Console.WriteLine("Function 2");
}

private static void function3() {
Console.WriteLine("Function 3");
}
}
}

Although it's not necessary to create your own delegate type. You can
use the built in Action type instead:

public static void StartWorking(Action MyFunction) {
//code here
}

But Action is nothing more than a delegate type.

Chris
Sep 16 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.