views:

59

answers:

2

I am just a beginner."ParameterizedThreadStart" accepts single object as argument.

Is there any other delegate signature allows me to

(1) pass params (variable number of parameter) on thread?

(2) support generic parameters like list ?

+2  A: 

You can use Delegate.BeginInvoke and EndInvoke, to pass any parameters you want

delegate long MyFuncDelegate(int N );

MyFuncDelegate cpn = new MyFuncDelegate(MyFunc); 

IAsyncResult ar = cpn.BeginInvoke( 3, null, null ); 

// Do some stuff 
while( !ar.IsCompleted ) 
{ 
    // Do some stuff 
} 

// we now know that the call is 
// complete as IsCompleted has 
// returned true 
long answer = cpn.EndInvoke(ar);
ArsenMkrt
+5  A: 

You can do anything you want with a single object. Just define a class to wrap the parameters you are interested in:

class ThreadState
{
    public ThreadState()
    {
    }

    public string Name
    {
        get;
        set;
    }

    public int Age
    {
        get;
        set;
    }
}

// ...

ParameterizedThreadStart start = delegate(object objThreadState)
{
    // cast to your actual object type
    ThreadState state = (ThreadState)objThreadState;

    // ... now do anything you want with it ...
};
bobbymcr
Or just pass an array of objects.
Lasse V. Karlsen
Yes, you can pass an array too, which is more lightweight but less "type safe."
bobbymcr
Really helped me a lot.Thanks.