我只能关闭窗体一次,InvalidOperation异常调用或BeginInvoke不能调用控件上,直到窗口句柄已创建 [英] I can only close a form once, InvalidOperation Exception Invoke or BeginInvoke cannot be called on a control until the window handle has been created

查看:233
本文介绍了我只能关闭窗体一次,InvalidOperation异常调用或BeginInvoke不能调用控件上,直到窗口句柄已创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜 我是从我的主要形式打开窗体这样当用户进行选择的菜单项。

Hi I'm opening a form like this from my main form when the user makes a selection of a menu item.

private void commToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Command_Form Command_Form1 = new Command_Form();
            Command_Form1.ShowDialog();
           // Command_Form1.Dispose();    this didn't help
        }

里面的形式Command_Form1 我关闭它这样,当用户点击关闭按钮

Inside the form "Command_Form1" I close it like this when the user clicks on the close button

private void Close_button_Click(object sender, EventArgs e)
        {
          this.Close();    //I get the exception here 
        }

此过程能正常工作一次,但在表格的第二封闭 (我希望的形式是一个完全不同的/新的实例)我得到错误的这篇文章的标题。 这是在调试窗口中的输出。

This process works fine once but on the second closing of the form (Which I hope is a completely different/new instance of the form) I get the error in the title of this post. This is the output in the debug window.

第一个机会异常类型的'System.InvalidOperationException'在System.Windows.Forms.dll中发生

所有这些列出此错误去约没有试图做任何事情尚未显示的形式,但这种情况发生时,我点击的形式按钮的主题。 这似乎对我来说,pretty的多少保证形式已经显示出来,如果​​我可以点击相应的按钮。

All the topics that list this error go on about not trying to do anything to a form that has not been displayed but this happens when I click on a button in the form. It would seem to me that pretty much ensures the form has been displayed if I'm able to click its button.

我发现列表这种类型的错误继续有关使线程安全的电话,所以我想这是一个实验,但它并没有任何区别的其他职务。

The other posts I've found that list this type of error go on about making thread safe calls so I tried this as an experiment but it didn't make any difference.

private void Close_button_Click(object sender, EventArgs e)
            {
    if (this.InvokeRequired)
                {
                    CloseCallback d = new CloseCallback(Close_button_Click);
                    Invoke(d, new object[] { sender, e });
                }
                else
                {
                    this.Close();

我在我的应用程序的多个线程,但它们是由我使用的是不是我明确的控件创建。 通过委托[S]我被Marshling接收到的将数据从一个串口/从表单/发送数据。 这是有道理的,串行端口将运行在不同的线程不是形式,而是为什么会点击一个按钮在窗体上是不是形式????

I have multiple threads in my application but they are created by the controls I'm using not by me explicitly. I am passing data from a serial port to/from the form by Marshling the received/sent data via a delegate[s]. It makes sense that the serial port would run on a different thread than the form but why would a button click on a form be in a diffrent thread than the form????

整个线程的事情是非常confuzing 我如何找出线程起源在哪里,什么是在线程怎么回事,我没有明确创建? 为什么我需要通过一个委托来调用窗体的close方法? 到底是有什么我可以在这个多线程环境中做到这一点是线程安全如何知道是否我在做什么是不安全/安全的,如果我不知道是什么/在哪里/为什么/谁/时创建线程?

The whole thread thing is very confuzing How do I figure out what threads originated where and what is going on in the threads that I didn't create explicitly? Why would I need to invoke the form's close method via a delegate? Heck is there anything I can do in this multi threading environment that is thread safe How in do I know if what I'm doing is unsafe/safe if I don't know what/where/why/who/when is creating threads?

推荐答案

我的猜测是你的亲密()调用不抛出的异常的事,但之后发生的close()。你有没有跨进code与调试器时看到它被开除了?

My guess is your close() call is not throwing that exception, but something that happens after close(). Have you stepped into the code with the debugger to see when it is fired?

正如当你需要调用到......只有一个允许做在GUI上的变化和访问动态属性,称之为GUI线程的线程。 GUI线程负责更新布局,像射击按钮等事件,如果你从另一个线程访问GUI(如定时器事件),你需要使用的invoke()排队的功能要在GUI线程上运行。 BeginInvoke的也将排队的功能,而且是异步的(只能排队功能被GUI线程上运行,但不会等待它完成)。

As to when you need to invoke...There is only one thread allowed to make changes and access dynamic properties on the GUI, call it the GUI thread. The GUI thread is responsible for updating layout, firing events like buttons, etc. If you ever access the GUI from another thread (like a timer event) you need to use invoke() to queue your function to be run on the GUI thread. BeginInvoke will also queue the function but is asynchronous (will only queue the function to be run on GUI thread, but will not wait for it to finish).

Close_button_click只能由你的GUI线程调用当按钮点击事件触发(除非你明确地把它叫做别的地方在你的code的背后,不推荐!),所以invokeRequired =假在code以上,并且永远不会执行的调用code。

Close_button_click will only be called by your gui thread when the button click event fires(unless you explicitly call it somewhere else in your code behind, not recommended!), so invokeRequired=false in your code above, and the invoke code is never executed.

这篇关于我只能关闭窗体一次,InvalidOperation异常调用或BeginInvoke不能调用控件上,直到窗口句柄已创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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