[HttpGet]
        public async Task<string> TestUU()
        {
            ParentTsk().Wait();
            Console.WriteLine("Hello World!");
            return "ok";
        }
     public async static Task ParentTsk()
        {
            CancellationTokenSource cts = new CancellationTokenSource();
            cts.CancelAfter(20);
            Task<string> workTsk = Task.Run(async () => await WorkTsk());
            Task<string> watchTsk = Task.Run(async () => await WatchTsk(cts.Token));
            Task<string>[] tskArray = new Task<string>[] { watchTsk, workTsk };

            var rsltTsk = await Task.WhenAny(tskArray);
            Console.WriteLine($"执行结果{rsltTsk.Result}");

        }
     public async static Task<string> WorkTsk()
        {

            string fileName = @"d:\tstFile.txt";
            List<int> lstINt = Enumerable.Range(0, 5000).ToList();
            foreach (var item in lstINt)
            {
                await System.IO.File.AppendAllTextAsync(fileName, $"{item}|");
            }

            return "it is WorkTsk";
        }

        public async static Task<string> WatchTsk(CancellationToken token)
        {
            while (!token.IsCancellationRequested)
            {
                await Task.Delay(10);
            }
            return "it is WatchTsk";
        }

方案二

using System;
using System.Threading;

class Program
{
    static ManualResetEvent mreA = new ManualResetEvent(false);
    static ManualResetEvent mreB = new ManualResetEvent(false);

    static void Main()
    {
        Thread threadA = new Thread(MethodA);
        Thread threadB = new Thread(MethodB);

        threadA.Start();
        threadB.Start();

        // 等待任一线程完成
        WaitHandle.WaitAny(new WaitHandle[] { mreA, mreB });

        // 如果线程 A 先完成,则中断线程 B
        if (mreA.WaitOne(0))
        {
            threadB.Interrupt();
        }
        // 如果线程 B 先完成,则中断线程 A
        else if (mreB.WaitOne(0))
        {
            threadA.Interrupt();
        }
    }

    static void MethodA()
    {
        try
        {
            Console.WriteLine("MethodA 开始执行");
            // 模拟 MethodA 的执行时间
            Thread.Sleep(3000);
            Console.WriteLine("MethodA 执行完成");
            mreA.Set(); // 通知主线程线程 A 已完成
        }
        catch (ThreadInterruptedException)
        {
            Console.WriteLine("MethodA 被中断");
        }
    }

    static void MethodB()
    {
        try
        {
            Console.WriteLine("MethodB 开始执行");
            // 模拟 MethodB 的执行时间
            Thread.Sleep(2000);
            Console.WriteLine("MethodB 执行完成");
            mreB.Set(); // 通知主线程线程 B 已完成
        }
        catch (ThreadInterruptedException)
        {
            Console.WriteLine("MethodB 被中断");
        }
    }
}

 方案三

using System;
using System.Threading;

class Program
{
    static CancellationTokenSource ctsA = new CancellationTokenSource();
    static CancellationTokenSource ctsB = new CancellationTokenSource();

    static void Main()
    {
        Thread threadA = new Thread(() => MethodA(ctsA.Token));
        Thread threadB = new Thread(() => MethodB(ctsB.Token));

        threadA.Start();
        threadB.Start();

        while (true)
        {
            // 检查线程A是否完成
            if (threadA.ThreadState == ThreadState.Stopped)
            {
                Console.WriteLine("线程A先执行完毕,取消线程B");
                ctsB.Cancel();
                break;
            }
            // 检查线程B是否完成
            else if (threadB.ThreadState == ThreadState.Stopped)
            {
                Console.WriteLine("线程B先执行完毕,取消线程A");
                ctsA.Cancel();
                break;
            }
        }
    }

    static void MethodA(CancellationToken token)
    {
        try
        {
            Console.WriteLine("线程A开始执行");
            // 模拟 MethodA 的执行时间
            Thread.Sleep(3000);
            Console.WriteLine("线程A执行完成");
        }
        catch (ThreadInterruptedException)
        {
            Console.WriteLine("线程A被中断");
        }
    }

    static void MethodB(CancellationToken token)
    {
        try
        {
            Console.WriteLine("线程B开始执行");
            // 模拟 MethodB 的执行时间
            Thread.Sleep(2000);
            Console.WriteLine("线程B执行完成");
        }
        catch (ThreadInterruptedException)
        {
            Console.WriteLine("线程B被中断");
        }
    }
}

方案四

using System;
using System.Threading;

class Program
{
    static ManualResetEventSlim threadACompleted = new ManualResetEventSlim(false);
    static ManualResetEventSlim threadBCompleted = new ManualResetEventSlim(false);

    static void Main()
    {
        Thread threadA = new Thread(MethodA);
        Thread threadB = new Thread(MethodB);

        threadA.Start();
        threadB.Start();

        // 等待任一线程完成
        WaitHandle.WaitAny(new WaitHandle[] { threadACompleted.WaitHandle, threadBCompleted.WaitHandle });

        // 如果线程 A 先完成,则中断线程 B
        if (threadACompleted.IsSet)
        {
            threadB.Interrupt();
        }
        // 如果线程 B 先完成,则中断线程 A
        else if (threadBCompleted.IsSet)
        {
            threadA.Interrupt();
        }
    }

    static void MethodA()
    {
        try
        {
            Console.WriteLine("线程A开始执行");
            // 模拟 MethodA 的执行时间
            Thread.Sleep(3000);
            Console.WriteLine("线程A执行完成");
            threadACompleted.Set(); // 通知主线程线程 A 已完成
        }
        catch (ThreadInterruptedException)
        {
            Console.WriteLine("线程A被中断");
        }
    }

    static void MethodB()
    {
        try
        {
            Console.WriteLine("线程B开始执行");
            // 模拟 MethodB 的执行时间
            Thread.Sleep(2000);
            Console.WriteLine("线程B执行完成");
            threadBCompleted.Set(); // 通知主线程线程 B 已完成
        }
        catch (ThreadInterruptedException)
        {
            Console.WriteLine("线程B被中断");
        }
    }
}

 

posted on 2023-06-28 08:41  白码一号  阅读(241)  评论(0编辑  收藏  举报