如何从主线程外部启动和停止主线程?

时间:2015-02-02 13:24:41

标签: c# multithreading

我想从子线程开始或停止主线程......

有可能吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication3
{
    class Program
    {

        static int threadid;
        static async void create()
        {
            Console.WriteLine("from child");
            Thread mainthread = new Thread();//throws error
            mainthread.ManagedThreadId = threadid;//throws error here  
           if (mainthread.ThreadState==System.Threading.ThreadState.Suspended)
            mainthread.Resume();

        }
        static void Main(string[] args)
        {
            ThreadStart creat_ = new ThreadStart(create);
            Thread create_thread = new Thread(creat_);
            create_thread.Start();
             threadid = Thread.CurrentThread.ManagedThreadId;
            Thread.CurrentThread.Suspend();
            create_thread.Abort();
        }
    }
}

在上面的代码中,create_thread是子线程,我想停止从create_thread启动/恢复主线程。

1 个答案:

答案 0 :(得分:-1)

快速回答是:

Process.GetCurrentProcess().Kill();

但问题是你为什么要这样做?这使您无需在退出前清理任何文件或保存数据。