本文介绍了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

可以解决吗?

推荐答案

鉴于您的代码,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错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 22:21