System.InvalidOperationException错误 [英] System.InvalidOperationException error

查看:532
本文介绍了System.InvalidOperationException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的我收到此错误:

System.InvalidOperationException

  HResult = 0x80131509

 消息=跨线程操作无效:控制从其创建的线程以外的线程访问的"DimMeaBTN"。

  Source = System.Windows.Forms

  StackTrace:

    at System.Windows.Forms.Control.get_Handle()

    at System.Windows.Forms.Control.OnEnabledChanged(EventArgs e)

    at System.Windows.Forms.ButtonBase.OnEnabledChanged(EventArgs e)

    at System.Windows.Forms.Control.set_Enabled(布尔值)

    at Pixelation_Tool.Form1.Baseline()in C:\ Users\bboone\source \repos\Pixelation Tool \Pixelation Tool \ Form1.cs:第393行$
  ;   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)

    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx)

    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx)

    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state)

    at System.Threading.ThreadHelper.ThreadStart()

System.InvalidOperationException
  HResult=0x80131509
  Message=Cross-thread operation not valid: Control 'DimMeaBTN' accessed from a thread other than the thread it was created on.
  Source=System.Windows.Forms
  StackTrace:
   at System.Windows.Forms.Control.get_Handle()
   at System.Windows.Forms.Control.OnEnabledChanged(EventArgs e)
   at System.Windows.Forms.ButtonBase.OnEnabledChanged(EventArgs e)
   at System.Windows.Forms.Control.set_Enabled(Boolean value)
   at Pixelation_Tool.Form1.Baseline() in C:\Users\bboone\source\repos\Pixelation Tool\Pixelation Tool\Form1.cs:line 393
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

这是我的代码:

 void Baseline()
        {
            tcClient.WriteAny(statenumber, Int16.Parse("1"));
            Thread.Sleep(300);
            while(state != 2)
            {
            
            }
            running(1);

            tcClient.WriteAny(statenumber, Int16.Parse("4"));
            Thread.Sleep(300);
            while (state != 2)
            {

            }
            running(2);
            DimMeaBTN.Enabled = true;
            MessageBox.Show("Take Baseline Measurement has!");
        }

该函数在另一个线程中调用

that function is called in the another thread

可以解决吗?

推荐答案

当你打电话给你时会出现这个错误针对在UI线程上创建的对象的非UI线程上的方法。无法在创建它们的线程之外的任何线程上访问UI元素。如果你尝试那么你(最终)得到这个错误。解决方案
是为了确保在非UI线程上运行的任何代码都不会尝试更新UI。

You will get that error when you call a method on a non-UI thread against an object created on the UI thread. UI elements cannot be accessed on any thread other than the thread that created them. If you try then you (eventually) get this error. The solution is to ensure any code running on the non-UI thread never tries to update the UI.

鉴于您的代码,DimMeaBTN似乎是一个UI元素。因此,您无法在UI线程外更新它。如果你真的需要坚持你拥有的代码,那么你需要将请求编组到UI线程。

Given your code it appears that DimMeaBTN is a UI element. Therefore you cannot update it outside the UI thread. If you really need to stick with the code you have then you'll need to marshal the request to the UI thread.

//Not tested...
running(2);

EnableButton(DimMeaBTN, true);
...


delegate void ControlDelegate ( Control control, bool enable );

void EnableButton ( Control control, bool enable )
{
  if (control.InvokeRequired)
     control.Invoke((ControlDelegate)EnableButton, new [] { control, enable });
  else
     control.Enabled = enable;
}


最好将此切换为异步方法,以便混合和匹配。但这会影响调用树一直支持你的root调用,所以它非常广泛。

Preferably though you'd switch this to an async method which would allow you to mix and match. But that would impact the call tree all the way back up to your root calls so it is pretty extensive.


这篇关于System.InvalidOperationException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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