The method below is what I want to be done in that thread:
public void Startup(int port,string path) { Run(path); CRCCheck2(); CRCCheck1(); InitializeCodeCave((ushort)port); }
I tried what I could find googling,but nothing worked
public void Test(int port,string path) { Thread t = new Thread(Startup(port,path)); } public void TestA(int port,string path) { Thread t = new Thread(Startup); t.Start (port,path); }
Both don't compile,how to do that?
A thread can be created by implementing the Runnable interface and overriding the run() method. Then a Thread object can be created and the start() method called. The Main thread in Java is the one that begins executing when the program starts.
Discord introduced Threads, temporary text channels that will automatically disappear after they have been inactive for a specific length of time. You can create a Thread using any existing message, as well as from scratch. This feature allows you to organize different conversations within a single channel.
The following ways work.
// The old way of using ParameterizedThreadStart. This requires a // method which takes ONE object as the parameter so you need to // encapsulate the parameters inside one object. Thread t = new Thread(new ParameterizedThreadStart(StartupA)); t.Start(new MyThreadParams(path, port)); // You can also use an anonymous delegate to do this. Thread t2 = new Thread(delegate() { StartupB(port, path); }); t2.Start(); // Or lambda expressions if you are using C# 3.0 Thread t3 = new Thread(() => StartupB(port, path)); t3.Start();
The Startup methods have following signature for these examples.
public void StartupA(object parameters); public void StartupB(int port, string path);
Update The currently suggested way to start a Task is simply using Task.Run()
Task.Run(() => foo());
Note that this method is described as the best way to start a task see here
Previous answer
I like the Task Factory from System.Threading.Tasks. You can do something like this:
Task.Factory.StartNew(() => { // Whatever code you want in your thread });
Note that the task factory gives you additional convenience options like ContinueWith:
Task.Factory.StartNew(() => {}).ContinueWith((result) => { // Whatever code should be executed after the newly started thread. });
Also note that a task is a slightly different concept than threads. They nicely fit with the async/await keywords, see here.
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