日期:2014-05-18  浏览次数:20888 次

C#界面卡死问题
各位高手
  我在界面1和界面2转换时总出现当按下切换按钮后界面卡死在当前界面,像死机一样,不知该怎么解决,希望各位高手能帮忙解决,谢谢了,以下是我的转换程序,希望各位帮忙修改。
 private void Switch_Click(object sender, EventArgs e)
  {
  try
  {
  comm.DataReceived -= comm_DataReceived;//取消注册串口接收事件
  comm.Close();//关闭串口
  }  
  finally
  {
   
  Canshu_Class.flag = true;

  this.Close();//关闭当前界面
  DanMian d1 = new DanMian();
  d1.ShowDialog();//打开新界面
   
  }
  }

------解决方案--------------------
试试BackgroundWorker
C# code

private void Switch_Click(object sender, EventArgs e)
{
    BackgroundWorker bgWorker = new BackgroundWorker();

    bgWorker.DoWork += new DoWorkEventHandler((sender1, e1) =>
    {
        try
        {
            comm.DataReceived -= comm_DataReceived;//取消注册串口接收事件
            comm.Close();//关闭串口
        }
        finally {  }
    });

    bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler((sender2, e2) =>
    {
        Canshu_Class.flag = true;

        this.Close();//关闭当前界面
        DanMian d1 = new DanMian();
        d1.ShowDialog();//打开新界面
    });

    bgWorker.RunWorkerAsync();
}