Stopping a Threaded C# Plugin in editor when u stop play

Hi guys i need help i have a threaded plugin running and it keeps running while in editor and I’ve stopped the play how can i call this thread to stop so it doesn’t continue on after I’ve unplayed in the editor i have to kill the editor to make it stop to test my next changes.

Here’s my code for an example of how i start the thread:

 void Start()
    {

        Console.print("STARTING UDP CLIENT");
        //Creating Threads
        Thread t1 = new Thread(Method1)
        {
            Name = "Thread1"
        };
        Thread t2 = new Thread(Method2)
        {
            Name = "Thread2"
        };

        //Executing the methods
        t1.IsBackground = true;
        t1.Start();
        t2.IsBackground = true;
        t2.Start();

    }

    void Method1() {
   
    }
    void Method2() {
        AsynchronousClient h = new AsynchronousClient();
        h.name = name;
        h.StartClient();
        Console.print("STARTED UDP CLIENT On Thread 2");
    }

Hi Again Fixed my Own Issues but ill leave this here for some one else.

Here is my working code:

    public string name;
    Thread t1;
    Thread t2;
    // Start is called before the first frame update
    void Start()
    {

        Console.print("STARTING UDP CLIENT");
        //Creating Threads
         t1 = new Thread(Method1)
        {
            Name = "Thread1"
        };
         t2 = new Thread(Method2)
        {
            Name = "Thread2"
        };

        //Executing the methods
        t1.IsBackground = true;
        t1.Start();
        t2.IsBackground = true;
        t2.Start();

    }

    void Method1() {
   
    }
    void Method2() {
        AsynchronousClient h = new AsynchronousClient();
        h.name = name;
        h.StartClient();
        Console.print("STARTED UDP CLIENT On Thread 2");
    }

    void OnApplicationQuit()
    {

        t1.Abort();
        t2.Abort();

    }

Do not rely on Abort. The Abort method should be the absolute last resort. You should stop your thread gracefully by stopping whatever it currently does logically. We have no idea what your “AsynchronousClient” exactly does when you call “StartClient”. However it most likely has an infinite loop to process data. There are usually mechanism in such classes to actually stop / finish that loop.

Usually closing the actual socket used should terminate any waiting socket operation.

So first design your thread code in a way you can stop it properly. So you should “request” your threading code to stop whatever it does when you close / stop your game. You usually wait for the thread to finish by calling Join on the main thread, usually with a timeout. If the thread did not terminate within the timeout you can call Abort on the thread. Abort will actually throw an exception inside the thread. This can have unpredictable results and the thread isn’t even guaranteed to be terminated. So you really should design your code properly which includes to properly releasing system and OS resources.