当我试图关闭它时,Winform会冻结

Nig*_*ker 2 .net c# winforms

我有以下代码

private void btnClose_Click(object sender, System.EventArgs e)
{
     Close();
} // btnClose_Click
Run Code Online (Sandbox Code Playgroud)

运行close winforms函数后,应用程序冻结.知道为什么会这样吗?

我使用.net 2.0,我在Windows 7 vs2005下运行

编辑:

我在调试器中按下暂停后,我来了

private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
  // GUI
  if (FScannerThread_Running)
  {
    FScannerThread_Running = false;
    FScannerThread.Join();
  }
}
Run Code Online (Sandbox Code Playgroud)

它堆积在FScannerThread.Join();任何想法我怎么能杀死它?

Shi*_*rod 5

使用bool Thread.Join(int milliseconds)重载,如果结果为false,则使用Abort()您的线程.

private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    // GUI
    if (FScannerThread_Running)
    {
        FScannerThread_Running = false;
        if (!FScannerThread.Join(1000)) // Give the thread 1 sec to stop
        {
            FScanner.Abort();
        }    
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,您应该捕获ThreadAbortException您的线程以便优雅地结束它(例如,如果您有要发布的东西).