Function as parameter

  • Thread starter Thread starter Franck
  • Start date Start date
F

Franck

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 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
 
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
 
the delegate will work for any function with same amount and type and
order of parameter pass, the things is i may have a huge amount of
different function with different amount and type of parameter and i
need them to be used everywhere. so i would need to override the
delegate to accept all possibilities which would make the use of
delegate useless.

the ThreadStart option from Jon seems to be the best option and it
should work. Thanks
 
the delegate will work for any function with same amount and type and
order of parameter pass, the things is i may have a huge amount of
different function with different amount and type of parameter and i
need them to be used everywhere. so i would need to override the
delegate to accept all possibilities which would make the use of
delegate useless.

the ThreadStart option from Jon seems to be the best option and it
should work. Thanks

That's got exactly the same limitations though - a ThreadStart never
accepts any parameters. The point is that if you want to call a
function, and you're not going to pass in any arguments, it had better
be a function which doesn't take any parameters!

Put it this way: suppose you wanted to start a thread with a method
which *did* take a parameter, what would you expect that parameter's
value to be when the thread was started? If you want to supply the
arguments, you can create another delegate instance which will just
call the method, e.g. (using a lambda expression):

StartThread(() => MyMethod(50));

Jon
 
Back
Top