Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 导致应用程序终止的Thread.Abort()_C#_Wpf_Multithreading_Winforms - Fatal编程技术网

C# 导致应用程序终止的Thread.Abort()

C# 导致应用程序终止的Thread.Abort(),c#,wpf,multithreading,winforms,C#,Wpf,Multithreading,Winforms,我正在开发一个wpf应用程序,如果正在进行一些冗长的工作,它会向用户显示等待对话框。我已将等待对话框创建为从System.windows.Forms.form继承的windows窗体。当用户更改一些设置并应用时,我会显示这个等待对话框,让应用程序验证并保存新的配置。为了显示这个对话框,我创建了一个线程并调用了一个方法,该方法依次调用waitdialog表单的ShowDialog()方法。验证和保存设置后,我需要隐藏/取消此waitdialog表单。因此,Thread.Abort()用于中止显示w

我正在开发一个wpf应用程序,如果正在进行一些冗长的工作,它会向用户显示等待对话框。我已将等待对话框创建为从System.windows.Forms.form继承的windows窗体。当用户更改一些设置并应用时,我会显示这个等待对话框,让应用程序验证并保存新的配置。为了显示这个对话框,我创建了一个线程并调用了一个方法,该方法依次调用waitdialog表单的ShowDialog()方法。验证和保存设置后,我需要隐藏/取消此waitdialog表单。因此,Thread.Abort()用于中止显示waitdialog的线程

这样,当用户单击应用按钮保存新配置时,将显示等待对话框并正确隐藏,如果用户输入错误,将显示一个消息框。现在,如果用户再次单击应用按钮,应用程序将显示一个未处理的异常消息框,说明“线程正在中止”。为了解决此问题,我使用了以下方法:

1-在try/catch块中放入Thread.Abort()语句

2-在catch块中使用了Thread.ResetAbort()语句

3-应用程序启动时订阅的不同事件:

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
AppDomain.CurrentDomain.FirstChanceException += new EventHandler<System.Runtime.ExceptionServices.FirstChanceExceptionEventArgs>(CurrentDomain_FirstChanceException);
this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
System.Windows.Forms.Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException+=新的UnhandledExceptionEventHandler(CurrentDomain\u UnhandledException);
AppDomain.CurrentDomain.FirstChanceException+=新事件处理程序(CurrentDomain\u FirstChanceException);
this.dispatchernhandledexception+=新系统.Windows.Threading.dispatchernhandledexceptionventhandler(App_dispatchernhandledexception);
System.Windows.Forms.Application.ThreadException+=新系统.Threading.ThreadExceptionEventHandler(应用程序\u ThreadException);
但我还是吃不下那个例外。请提出一些解决这个问题的方法。因为它会导致异常并终止应用程序


这就是我应该如何处理它的。 将对wait对话框的调用放在一个方法中,例如

FORMWAITDIALOG _waitDialog=new FORMWAITDIALOG();
   void ShowDialog(){
        Thread t = new Thread(()=>{
        Invoke(new Action(()=>
        {
        _waitDialog.show();
        }));
        });
        t.start();
     }
然后在validate按钮的click事件中调用此方法


然后在exit方法中调用
\u waitDialog.hide()

哇<代码>线程.中止
是否隐藏表单?为什么不直接使用
Show
而不是
ShowDialog
,然后调用
Close
Hide
?不要使用Thread.Abort()。。。我建议使用带有取消令牌的任务,而不是直接使用线程…
Thread.Abort()
应该真正命名为
Thread.CreateNastyBug()
。@himanshu长时间运行操作的标准做法是使用show dialog在主线程上显示对话框,这样可以很好地暂停与非模态UI的交互。处理应该在第二个线程中完成。您不需要线程,因此也不需要Abort()。使用计时器。“几秒钟”的延迟是另一个问题,我们没有足够的信息。