Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# ParameterizedThreadStart not supported in Visual Studio 2003?

I am doing a multi-threaded application in C# and need to pass some parameters to a thread.

Attempting to use the ParameterizedThreadStart class so that I can pass a class variable (which contains all the parameters I want to pass). However, the class seems to be unrecognised (there is a red line underneath it in the source code) and on compilation I got a "The type or namespace name ParameterizedThreadStart could not be found (are you missing a using directive or an assembly reference?)".

I am using the following libraries from the framework: using System; using System.Collections; using System.Threading;

Am I doing anything wrong? I am using VS 2003 (7.1.6030) and .NET Framework 1.1.

Thank you.

like image 913
Andy Avatar asked May 26 '26 11:05

Andy


2 Answers

That ParameterizedThreadStart type not exists before .net framework 2.0, but you can accomplish the same goal (pass parameters to thread) creating a simple class and using good old ThreadStart delegate, like the following:

class ParamHolder
{
    private object data;

    public ParamHolder(object data) // better yet using the parameter(s) type
    {
        this.data = data;
    }

    public void DoWork()
    {
        // use data
        // do work
    }
}

and the use will be:

...
object param = 10; // assign some value
Thread thread = new Thread(new ThreadStart(new ParamHolder(param).DoWork));
thread.Start();
...
like image 75
Alex LE Avatar answered May 28 '26 01:05

Alex LE


The old way of doing this is to write a class to represent the state, and put the method there:

class MyState {
    private int foo;
    private string bar;
    public MyState(int foo, string bar) {
        this.foo = foo;
        this.bar = bar;
    }
    public void TheMethod() {...}
}
...
MyState obj = new MyState(123,"abc");
ThreadStart ts = new ThreadStart(obj.TheMethod);
like image 39
Marc Gravell Avatar answered May 28 '26 00:05

Marc Gravell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!