从另一个线程调用Windows窗体(.NET)窗体、线程、Windows、NET

2023-09-03 01:01:20 作者:相逢何必曾相识

您好我正在开发一个.NET应用程序,我想实现以下目标:

我有一个WinForms应用程序,和一个计时器(System.Timers.Timer的)是excecutes根据时间表线程。问题是,我无法从辅助线程(的WorkerThread)访问UI(windows窗体),错误说类似组件不能从一个线程访问了没有创造它。

有什么办法来实现这一目标?

谢谢!

解决方案

  formObject.Invoke(委托{
      //行动UI线程上执行
 });
 

Hi I'm developing a .Net application and I want to achieve the following:

WFP做的程序,调用线程无法访问此对象,因为另一个线程拥有该对象

I have a winforms application, and a timer (System.Timers.timer) that excecutes a thread based on a schedule. The problem is that I cannot access the UI (windows form) from the secondary thread (WorkerThread), the error say something like the component cannot be accessed from a thread that didn't create it.

Is there any way to achieve this?

Thanks!

解决方案

 formObject.Invoke(delegate { 
      // action to perform on UI thread
 });