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
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 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.
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.
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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With