Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start Thread with lambda expression c#

I have this class:

public class Statistics
{
    List<string> _lsit;

    public List<string> ipList
    {
        get { return _lsit; }
        set { _lsit = value; }
    }

    string _Path = @"C:\Program Files\myApp.exe";
    string _Path = "";
    ProcessStartInfo ps = null;

    public getStatistics(string Path)
    {
        _Path = Path;
        getStatistics();
    }
}

I want to start the function Statistics with different Thead and i did somthing like:

Statistics stat = new Statistics (some path);
Thread<List<string>> lList = new Thread<List<string>>(() => tsharkIps.getStatistics());

but the compiler Error says "The non-generic type 'System.Threading.Thread' cannot be used with type arguments"

I did not write all my class and only want to know hot to start the thread

thanks

like image 562
user979033 Avatar asked Mar 06 '12 07:03

user979033


People also ask

Can we create thread using lambda expression?

Create the Runnable interface reference and write the Lambda expression for the run() method. Create a Thread class object passing the above-created reference of the Runnable interface since the start() method is defined in the Thread class its object needs to be created. Invoke the start() method to run the thread.

Can a lambda closure be used to create a C ++ 11 thread?

Can you create a C++11 thread with a lambda closure that takes a bunch of arguments? Yes – just like the previous case, you can pass the arguments needed by the lambda closure to the thread constructor.

How do I start a thread in C#?

Create New Thread [C#] First, create a new ThreadStart delegate. The delegate points to a method that will be executed by the new thread. Pass this delegate as a parameter when creating a new Thread instance. Finally, call the Thread.

Are lambda expressions thread safe C#?

This is a question that can only be answered by knowing the contents of the lambda expression. They are not inherently thread safe. In my experience the are much less likely to be thread safe than you would expect. s/main thread/current thread/ as its wherever the delegated is executed.


1 Answers

You need to take a step back to start with and read the compiler error. Thread is not a generic type. It's really not at all clear what you're trying to do here, especially as you haven't even shown a parameterless getStatistics() method (which should be called GetStatistics() to follow .NET naming conventions) and the parameterized getStatistics() method you have shown doesn't have a return type.

Starting a thread with a lambda expression is the easy part:

Thread thread = new Thread(() => CallSomeMethodHere());
thread.Start();

It's not clear how that translates to your sample code though.

Or using the TPL in .NET 4, you can (and probably should use Task or Task<T>):

Task task = Task.Factory.StartNew(() => CallSomeMethodHere());

or

Task<string> task = Task.Factory.StartNew(() => CallSomeMethodReturningString());

It's possible that you really want:

Task<List<string>> statisticsTask = Task.Factory.StartNew(() => {
   Statistics statistics = new Statistics(path);
   return statistics.ipList();
});

Note that here the constructor is called within the new task - which is important, as it looks like that's probably doing all the work. (That's usually a bad idea to start with, but that's another matter.)

You should look at .NET naming conventions in general, btw...

like image 119
Jon Skeet Avatar answered Sep 28 '22 08:09

Jon Skeet