未处理Application.Run引发ArgumentException [英] Application.Run throws ArgumentException was unhandled

查看:84
本文介绍了未处理Application.Run引发ArgumentException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要关闭应用程序的条件,因此我将其命名为.在设置certian标志时将其处置().

I have a condition in which I need to close the application and so I call this.Dispose () when I set a certian flag.

起初,我认为在调用this.Dispose()之后调用函数是一个问题,因此我将代码移到最后一个调用的地方,但是我仍然收到"ArgumentException未处理"的问题. ."在Application.Run上(新的myApp();行.

At first I thought it was a problem of calling functions after I call this.Dispose () and so I moved the code to be the last thing called, but I still get an "ArgumentException was unhandled" "Parameter is not valid." On the Application.Run (new myApp (); line.

我做错了什么?我一路上错过了什么吗?或者也许有更好的方法来关闭应用程序?

What am I doing wrong? Did I miss something along the way? Or maybe there is a better way to close the application?

推荐答案

尝试使用Application.Exit()退出应用程序.

Try using Application.Exit() to exit the application.

使用Application.Run(new MyForm());时,使用窗体对象作为主窗体在线程上创建了一个消息循环.它尝试将即将到达应用程序的Win32消息传递到它们各自的对象.但是,当您在表单对象上调用Dispose()时,您尚未退出消息循环.当它尝试将下一条消息传递到您的表单对象时,由于它已经被处理并抛出异常而失败.您应该请求关闭表单(通过在表单上调用Close),然后将要求表单处理事件,如果完成,请在随后退出消息循环.另一种方法(更直接的方法)是通过调用Application.Exit()来完全关闭线程上的消息循环,这将导致所有相关形式都被关闭.

When you use Application.Run(new MyForm());, a message loop is created on the thread using the form object as the main form. It tries to deliver Win32 messages that are coming to the application to their respective objects. However, when you call Dispose() on the form object, you haven't exited the message loop yet. When it tries to deliver the next message to your form object, it fails since it's already disposed and throws the exception. You should either request the form to be closed (by calling Close on the form), which will then ask the form to process the event and if completed, exit the message loop afterwards. The other way (more direct way) is to shut down the message loop on the thread altogether by calling Application.Exit() which will cause all related forms to be closed.

这篇关于未处理Application.Run引发ArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆