关闭窗体时的异常(thread + invoke)

编程入门 行业动态 更新时间:2024-10-26 19:35:19
本文介绍了关闭窗体时的异常(thread + invoke)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我刚刚开始学习c#中的线程和方法,但是遇到一个问题,我找不到解决方案。

我做了一个基本的C#表单程序,通过启动一个线程并调用委托来保持更新和显示一个数字。

在Form1_load上启动新线程:

private void Form1_Load(object sender,EventArgs e) {t = new System.Threading.Thread(DoThisAllTheTime); t.Start(); }

公共无效DoThisAllTheTime(不断更新号码):

public void DoThisAllTheTime() { while(true) { if(!this .IsDisposed) { number + = 1; MethodInvoker yolo = delegate(){label1.Text = number.ToString(); }; this.Invoke(yolo); } } }

现在当我点击X按钮的形式,我得到以下异常:

'System.Windows.Forms.dll中发生类型为System.ObjectDisposedException的未处理的异常

无法更新已删除的对象'

虽然我确实检查了表单是否被处理。 >

编辑:我将catch(ObjectDisposedException ex)添加到修复问题的代码中。 工作代码:

public void DoThisAllTheTime() { while(true) { number + = 1; try { MethodInvoker yolo = delegate(){label1.Text = number.ToString(); }; this.Invoke(yolo); } catch(ObjectDisposedException ex) { t.Abort(); } } }

解决方案

您致电 this.IsDisposed 始终过期。你需要拦截你的表单关闭事件并明确地停止线程。那么你根本就不必这样做 IsDisposed 测试。

有很多方法可以做到这一点。个人来说,我将使用 System.Threading.Tasks 命名空间,但如果您想保留使用 System.Threading ,你应该定义一个成员变量 _updateThread ,并在你的加载事件中启动它:

code> _updateThread = new System.Threading.Thread(DoThisAllTheTime); _updateThread.Start();

然后在关闭活动中:

private void Form1_Closing(object sender,CancelEventArgs e) { _stopCounting = true; _updateThread.Join(); }

最后,替换 IsDisposed 测试,检查新的 _stopCounting 成员变量的值:

public void DoThisAllTheTime() { MethodInvoker yolo = delegate(){label1.Text = number.ToString(); }; while(!_ stopCounting) { number + = 1; this.Invoke(yolo); } }

I have just started to learn about threads and methodinvoking in c#, but I have come across a problem which I couldn't find the solution of.

I made a basic C# form program which keeps updating and displaying a number, by starting a thread and invoke delegate.

Starting new thread on Form1_load:

private void Form1_Load(object sender, EventArgs e) { t = new System.Threading.Thread(DoThisAllTheTime); t.Start(); }

Public void DoThisAllTheTime (which keeps updating the number) :

public void DoThisAllTheTime() { while(true) { if (!this.IsDisposed) { number += 1; MethodInvoker yolo = delegate() { label1.Text = number.ToString(); }; this.Invoke(yolo); } } }

Now when I click the X button of the form, I get the following exception:

'An unhandled exception of type 'System.ObjectDisposedException' occurred in System.Windows.Forms.dll

Can't update a deleted object'

While I actually did check if the form was disposed or not.

EDIT: I added catch (ObjectDisposedException ex) to the code which fixed the problem. Working code:

public void DoThisAllTheTime() { while(true) { number += 1; try { MethodInvoker yolo = delegate() { label1.Text = number.ToString(); }; this.Invoke(yolo); } catch (ObjectDisposedException ex) { t.Abort(); } } }

解决方案

Your call to this.IsDisposed is always out of date. You need to intercept your form closing event and stop the thread explicitly. Then you won't have to do that IsDisposed test at all.

There are many ways you can do this. Personally, I would use the System.Threading.Tasks namespace, but if you want to keep your use of System.Threading, you should define a member variable _updateThread, and launch it in your load event:

_updateThread = new System.Threading.Thread(DoThisAllTheTime); _updateThread.Start();

Then in your closing event:

private void Form1_Closing(object sender, CancelEventArgs e) { _stopCounting = true; _updateThread.Join(); }

Finally, replace the IsDisposed test with a check on the value of your new _stopCounting member variable:

public void DoThisAllTheTime() { MethodInvoker yolo = delegate() { label1.Text = number.ToString(); }; while(!_stopCounting) { number += 1; this.Invoke(yolo); } }

更多推荐

关闭窗体时的异常(thread + invoke)

本文发布于:2023-11-11 16:53:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579023.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:窗体   异常   invoke   thread

发布评论

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

>www.elefans.com

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