未处理Application.Run引发ArgumentException

编程入门 行业动态 更新时间:2024-10-28 22:29:33
本文介绍了未处理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

本文发布于:2023-06-04 21:17:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/505223.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:未处理   Application   ArgumentException   Run

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!