Why doesn't .Net have a generic Thread.Start () version?

I'm wondering why .Net doesn't have a generic method to start a thread.

For example, we start a thread as shown below.

Thread th = new Thread(SayHello);
th.Start("Hello");

private static void SayHello(object obj)
        {
            string str = obj as string;
            Console.WriteLine(str);
        }

      

Why can't we keep in mind why the .Net team didn't consider it universal?

Something like the following ...

Thread<string> th = new Thread<string>(SayHello);

      

Since many times when I passed value types to the beginning of the stream I need to do boxing / unboxing.

+3


source to share


6 answers


I see several reasons why people who implement BCL haven't written something like this:

  • If you are concerned about performance, the cast (even when unpacking it) will be much less than the actual creation of the stream.
  • If you are worried about security types, you can easily use a lambda: new Thread(() => SayHello("Hello"))

    .
  • Thread<T>

    will be confusing type because it is not clear what this means T

    . Moreover, it would have a completely different meaning than T

    in Task<T>

    .
  • If you want, you can create yours Thread<T>

    as a wrapper using 20 lines (see below).

So the problem is tiny and easy to work with if it really bothers you and the solution can be misleading. This is most likely why resources were not spent on it.




Possible implementation Thread<T>

:

class Thread<T>
{
    private readonly Action<T> m_action;
    private readonly Thread m_thread;
    private T m_parameter;

    public Thread(Action<T> action)
    {
        m_action = action;
        m_thread = new Thread(DoWork);
    }

    public void Start(T parameter)
    {
        m_parameter = parameter;
        m_thread.Start();
    }

    private void DoWork()
    {
        m_action(m_parameter);
    }
}

      

+4


source


Because nobody has written this method yet. Also, since boxing / unboxing is a fairly small part of the overall operation here, and few programmers really have to start the thread manually (in most use cases, abstraction is better), they probably needed little to point out, implement, test, and document the change that the substance is not required.



+2


source


It's very easy to start a stream using a lambda to use strong typing, so they don't need to add any new language support for it.

For example:

using System;
using System.Threading;

class Program
{
    static void Main(string[] args)
    {
        // Easy way to start a thread with strongly-typed multiple parameters:

        new Thread(() => MyThreadFunc("Test", 42, DateTime.Now)).Start();

        Console.WriteLine("Started thread.");
        Thread.Sleep(2000);
    }

    static void MyThreadFunc(string param1, int param2, DateTime param3)
    {
        Thread.Sleep(1000);
        Console.WriteLine("param1 = {0}, param2 = {1}, param3 = {2}", param1, param2, param3);
    }
}

      

+2


source


There are many classes in .net that I believe will have a common version if developed after the introduction of generics. The topic is just one of them.

For this specific problem, if you can use .net 4.0+:

Use the Task class in the System.Threading.Tasks Namespace.

private static void SayHello(string s)
{
    Task t = new Task(() => Console.WriteLine(s));
    t.Start();
}

      

This example is type safe and does what your example did. This approach is different and the task makes a lot more sense if you expect the result, but it's still a good replacement, I think, even here. And my guess is that MS won't run in the "old" Thread class, but may continue to improve the parallel task library .

+1


source


Due to "historical reasons". This method was created in .NET 1 (no generics) and has not been fixed since. Same as ICloneable etc.

0


source


weird .. nobody asked why anyone wants to do what the OP is asking. thread is for running a piece of code. thread is not a method. due to the nature of generics in .NET, the new Thread () code would mean creating an object with a specific type string. it has nothing to do with the Thread class running for a while and doing some things. complex material.

to illustrate my idea - it's the same as if he asked - why don't we have steamers equipped with TV and XBOX ...

so everything in my answer is not implemented because it contradicts the .NET architecture .. that is, is * wrong *, not because they didn't bother.

0


source







All Articles