Home > Mobile >  Interrupt Thread Child c#
Interrupt Thread Child c#

Time:12-20

How interrupt thread child in this case? I need interrupt foreach in thread child after throw exception in thread parent

            Thread child;

            Thread parent = new Thread(() =>            
            {

                child = new Thread(() =>
                {
                    for (int i = 0; i < int.MaxValue; i  )
                    {
                        Console.WriteLine("THREAD 1 = "   i);
                    }
                });

                child.Start();

                try
                {
                    child.Join();
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine(ex.ToString());

**                    // here????**

                }
            });

            parent.Start();

            parent.Interrupt();

Interrupt thread child

CodePudding user response:

You cannot "interput" threads. Older .Net versions had Thread.Abort, but there are several good reasons why it should never be used.

What you should do is ask the thread to stop processing, and your thread should frequently check if needs to stop.

There are several ways to do this, one example would be the cancellation token. Let your thread take a cancellation token as input and in your loop add a call to

myCancellationToken.ThrowIfCancellationRequested();

Your caller would create a cancellation token source to generate the token and call .Cancel() to request the thread to abort.

However, using threads directly is very outdated, you should be using task based programming. There are common patterns to do most common tasks in a elegant and compact way, like DataFlow, Parallel.For, PLinq, concurrent collections etc.

Multi threaded programming is difficult. You need to have a fair bit of knowledge on the topic before you try to write anything intended for real world use. Thread safety bugs are quite easy to make, and are notoriously difficult to find and debug. So I would recommend studying thread safety, as well as modern threading patterns. This is not an area where trial and error is appropriate, since you are likely to create thing that work 99.9% of the time, or works 100% on your computer, but not in production.

CodePudding user response:

JonasH's answer is a good one, but just for reference, here's how to use the CancellationToken with the original code:

CancellationTokenSource cts = new CancellationTokenSource();
CancellationToken ct = cts.Token;

Thread parent = new Thread(() =>
{

    Thread child = new Thread(() =>
    {
        for (int i = 0; i < int.MaxValue; i  )
        {
            if (ct.IsCancellationRequested)
            {
                break;
            }
            Console.WriteLine("THREAD 1 = "   i);
        }
    });

    child.Start();

    try
    {
        child.Join();
    }
    catch (Exception ex)
    {
        System.Console.WriteLine(ex.ToString());
        cts.Cancel();
    }
});

parent.Start();
parent.Interrupt();
  • Related