为什么.NET不允许跨线程操作?不允许、线程、操作、NET

2023-09-04 01:10:38 作者:女施主,请自重

可能重复:    .NET控件:为什么AREN&rsquo的;吨的所有呼叫线程安全的

这个问题不是关于什么是一个跨线程操作,以及如何避免它,但为什么.NET框架的内部机制不允许跨线程操作。

This question is not about what is a cross-thread operation, and how to avoid it, but why internal mechanics of .NET framework does not allow a cross-thread operation.

我`吨明白为什么的SerialPort DataReceived事件检索无法更新我的形式简单的文本框,以及为什么使用委托这是可能的吗?

I can`t understand why a SerialPort DataReceived event cannot update a simple text box on my form and why using delegates this is possible?

推荐答案

.NET允许跨线程操作。但是,你应该安全地处理它。使用调用 / 的BeginInvoke 的方法进行跨线程操作。调用的意思是嘿,文本框!请更新自己,当你将有时间。下面是使用对MSDN

.NET allows cross-thread operations. But you should handle it safely. Use Invoke / BeginInvoke methods for cross-thread operations. Invoke means "Hey, text box! Please update yourself when you will have time." Here is an example of usage on MSDN