Reputation: 297
I want to terminate the thread thr1 in Program Class from another class, I created a PoC code as shown below to demonstrate it better. Assume my Program class is as below:
public class Program
{
public static Thread thr1;
public static void Main(string[] args)
{
Thread thr1= new Thread(() => Class1.DoSomething(param1,param2));
thr1.Start();
Thread.Sleep(1000);
Test.Exec();
}
}
and this is my Test class:
public class Test
{
public static void Exec()
{
Program.thr1.Abort();
Program.thr1= new System.Threading.Thread(() => Class1.DoSomething(param1, param2));
Program.thr1.Start();
}
}
What I actually want to do is to terminate the thr1 in class Program by the Exec method in Test class and reassign with different parameters and start it again. Is this possible?
EDITED: Maybe I should have asked how to access instantiated object from another class?
Upvotes: 0
Views: 664
Reputation: 3007
Try this
public class Program
{
public static Thread thr1;
public static void Main(string[] args)
{
thr1= new Thread(() => Class1.DoSomething(param1,param2));
thr1.Start();
Thread.Sleep(1000);
Test.Exec();
}
}
Test class
public class Test
{
public static void Exec()
{
Program.thr1.Abort();
Program.thr1= new System.Threading.Thread(() => Class1.DoSomething(param1, param2));
Program.thr1.Start();
}
}
Upvotes: 0
Reputation: 297
Solution, @Karan Mentioned:
Replacing
Thread thr1= new Thread(() => Class1.DoSomething(param1,param2));
with
thr1 = new Thread(() => Class1.DoSomething(param1,param2));
It should work as you've expected.
Edit
It was not working because in code declaring Thread thr1
inside Main
which creating new local instance and not accessible to other classes.
As your global object is static it will always give you same reference. So assigning as thr1 = new Thread(() => Class1.DoSomething(param1,param2));
will work fine.
Upvotes: 2
Reputation: 4282
As a general advice element avoid Thread Abort. It does not result in graceful shutdown and can lead to all kinds of issues. Most importantly make sure your thread is not responsible for keeping any relevant data intact. Thread.Abort will result in a ThreadAbortException being thrown and often does not result in what you would want to happen in all circumstances. If you do not control the code launched in the thread you may not have another option, but if you do, do not use this mechanism.
Consider using something like this. (Note the relevant part is you being able to gracefully terminate the work you are doing on the worker thread (sometimes this is difficult):
This is a C# 7.2 console app (as it has an async main method).
Just create this as a Console App to test, but if you do run it from an actual command line as VS sometimes doesn't play nice with CTRL-C interruption.
internal class Program
{
private static async Task Main(string[] args)
{
var cts = new CancellationTokenSource();
Console.WriteLine("Press CTRL-C to STOP!");
Console.CancelKeyPress += (sender, eventArgs) => cts.Cancel();
var longRunningTask = Task.Run(() => DoSomethingLongRunning(cts.Token));
while (!longRunningTask.IsCompleted)
{
await Task.Delay(50, cts.Token);
}
}
private static async Task DoSomethingLongRunning(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
await Task.Delay(1000, token);
Console.WriteLine("Doing Something");
}
}
}
Upvotes: 1