当其他线程完成时,窗体意外关闭.怎么预防? [英] Form closing unexpectedly when other thread finishes with it. How to prevent?

查看:69
本文介绍了当其他线程完成时,窗体意外关闭.怎么预防?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在编写一种方法,该方法将创建一个表单,然后慢慢更改此新表单的不透明度,直到其100%不透明为止.如果我在一个线程中完成所有操作(创建表单对象并更改不透明度),则它可以正常工作,并且拥有我的表单,尽管我必须等到它完成后才能在任一表单上执行任何操作...

So I'm writing a method that will create a form and then slowly change the opacity of this new form until it's 100% opaque. If I do it all in one thread (create the form object AND change the opacity) then it works and I have my form, although I have to wait until it's done before I can do anything on either form...


推荐答案

嘿,Corey,

您是在后台线程中说Form.Show,因此,当该线程存在时,Form将关闭.如果将Form.Show放在主线程中应该很好.

但是,最佳实践是,如果要启动单独的UI线程,则应派生自Thread类并使用该类.否则,如果要通过后台线程操作UI组件,则可能会获得CrossThreadIllegalOperation的异常.

让我知道是否有帮助.

谢谢,
Viral.
Hey Corey,

You are saying Form.Show in your Background Thread and so, when that thread exists the Form closes. If you put Form.Show in your main thread you should be good.

However, best practice is that if you want to start a seperate UI thread then you should derive from the Thread class and use that class. Otherwise, if you are manipulating UI components through background thread then, you may get Exception for CrossThreadIllegalOperation.

Let me know if that helps.

Thanks,
Viral.


这篇关于当其他线程完成时,窗体意外关闭.怎么预防?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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