在 m_process.Start();后面加上一句 Thread.CurrentThread.Join();

解决方案 »

  1.   

    要等结束可以在StartListening 线程结束时设置回调
      

  2.   

    用事件:
     public delegate void myreply();
    public event myreply my;this.my+=new myreply(处理的方法);在StartListening线程结束时this.my();
      

  3.   

    To:exboy(kuku) 应该是加 m_process.Join(); 吧,要等的是m_process 这个thread
    如果加Thread.CurrentThread.Join();就“死机”了,在当前运行的thread 里面等它自己完成不合逻辑,是吧:)
      

  4.   

    //proof of concept
    class Class1
    {
    static void Main(string[] args)
    {
    Thread t = new Thread(new ThreadStart(Run));
    t.Start(); Console.WriteLine("waiting for t to finish"); t.Join();//把这句换成下面一句,不会出现"finish waiting"
    //Thread.CurrentThread.Join(); Console.WriteLine("finish waiting"); Console.Read();
    } private static void Run()
    {
    Console.WriteLine("Thread t started");
    Thread.Sleep(1000);
    Console.WriteLine("Thread t finished");
    }
    }
      

  5.   

    我一个操作很长
    /// <summary>
    /// 停止服务,错误抛出错误
    /// </summary>
    public void Stop()
    {
    m_wnd.Log("停止服务");
    m_bState=false;
    try
    {
    if(m_process!=null && m_process.IsAlive)
    m_process.Abort(); //关闭监听进程
    m_server.Stop();
                    if(m_Clients!=null && m_Clients.Count!=0)
    {
    for(int i=0;i<m_Clients.Count;i++)
    {
    DawayJwtClient client=(DawayJwtClient)m_Clients[i];
    client.Dispose(); 这里时间长
    }
    m_Clients.Clear();
    }
    if(m_currentThread!=null && m_currentThread.IsAlive)
    m_currentThread.Abort();
    }
    catch(Exception err)
    {
    throw err;
    }
    }
    这个线程技术时我写入日志,好像与开始停止服务同时的,但我ui上等很久,怎么等他结束??
    // <summary>
    /// 关闭socket,释放资源
    /// </summary>
    public void Dispose()
    {
    if(this.Sock!=null && this.Sock.Connected)
    {
    this.Sock.Shutdown(SocketShutdown.Both);
    this.Sock.Close();
    }
    if(this.CLThread!=null && this.CLThread.IsAlive)
    this.CLThread.Abort();
    GC.SuppressFinalize(this);
    }