跨线程操作无效 [英] Cross-thread operation not is valid

查看:67
本文介绍了跨线程操作无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个较新的c#程序员,我不知道如何在我的运行错误中处理我的serach结果



调试时我一直收到以下错误。 />




跨线程操作无效:控制'  txtDataRx'从其创建的线程以外的线程访问





这是它所指的代码:





  public   void  OnDataReceived(IAsyncResult asyn)
{
try
{
CSocketPacket theSockId =(CSocketPacket)asyn.AsyncState;
// end receive ...
int iRx = 0 ;
iRx = theSockId.thisSocket.EndReceive(asyn);
char [] chars = new char [iRx + 1 ];
System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(theSockId.dataBuffer, 0 ,iRx,chars,< span class =code-digit> 0 );
System。 String szData = new System。(字符);
txtDataRx.Text = txtDataRx.Text + szData;
WaitForData(m_socWorker);
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log( 0 1 \ nOnnDataReceived:套接字已关闭\ n);
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}







有人可以请帮助我解决这个问题?

请!!!!!!!

解决方案

您正在从其他线程更新UI控件。



而不是直接更新它,



 txtDataRx.Text = txtDataRx.Text + szData; 





代替使用代表,



例如定义一个代表



 委托  void  UpdateTextBoxCallBack(TextBox txtBox, string  strText); 





创建一个方法,更新此文本框,如下所示。



  public   void  UpdateTextBox(TextBox txtBox, string  strText)
{
<跨度lass =code-keyword> if (txtBox.InvokeRequired)
{
UpdateTextBoxCallBack upcb = new UpdateTextBoxCallBack(UpdateTextBox);
this .Invoke(upcb, new object [] {txtBox,strText});
}
else
{
txtBox.Text = strText;
}
}







以及何时需要更新此文本框从其他一些线程。只需调用方法



 UpdateTextBox(txtBox, 要更新的新文本); 





希望这会有所帮助。



-praveen。


您只能从创建它的线程(即UI线程)访问该控件。在其他线程(如BackgroundWorker)中,您需要使用 Control.BeginInvoke [ ^ ]或 Control.Invoke [ ^ ]。



替换以下代码

< pre lang =c#> txtDataRx.Text = txtDataRx.Text + szData;



with

 < span class =code-keyword> this  .BeginInvoke( new  MethodInvoker( delegate ( )
{
this .txtDataRx.AppendText(szData);
}));



然后尝试它是否有效。



祝你好运


i am a newer c# programmer and i dont know how to work my serach result in my accourding error

I keep getting the following error when debugging.


Cross-thread operation not valid: Control 'txtDataRx' accessed from a thread other than the thread it was created on.



Here's the code that it points to:


public  void OnDataReceived(IAsyncResult asyn)
    {
        try
        {
            CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState ;
            //end receive...
            int iRx  = 0 ;
            iRx = theSockId.thisSocket.EndReceive (asyn);
            char[] chars = new char[iRx +  1];
            System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
            int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
            System.String szData = new System.String(chars);
            txtDataRx.Text = txtDataRx.Text + szData;
            WaitForData(m_socWorker );
        }
        catch (ObjectDisposedException )
        {
            System.Diagnostics.Debugger.Log(0,"1","\nOnDataReceived: Socket has been closed\n");
        }
        catch(SocketException se)
        {
            MessageBox.Show (se.Message );
        }
    }




Can somebody please help me fix this?
please!!!!!!!

解决方案

You are updating a UI control from a different thread.

instead of updating it directly,

txtDataRx.Text = txtDataRx.Text + szData;



instead use a delegate,

e.g define a delegate

delegate void UpdateTextBoxCallBack(TextBox txtBox, string strText);



create a method, to update this text box as shown below.

public void UpdateTextBox (TextBox txtBox, string strText)
{
      if (txtBox.InvokeRequired)
      {
          UpdateTextBoxCallBack upcb = new UpdateTextBoxCallBack(UpdateTextBox);
          this.Invoke(upcb, new object[] { txtBox, strText});
      }
      else
      {
           txtBox.Text = strText ;
      }
}




and when you need to update this text box from some other thread. just call the method

UpdateTextBox (txtBox, "New Text to be updated" );  



hope this helps.

-praveen.


You can only access the control from the thread where it was created(that is, the UI thread). In other threads(like your BackgroundWorker), you need to use Control.BeginInvoke[^] or Control.Invoke[^].

Replace your following code

txtDataRx.Text = txtDataRx.Text + szData;


with

this.BeginInvoke(new MethodInvoker(delegate()
{
   this.txtDataRx.AppendText(szData);
}));


and then try if it works.

Good luck


这篇关于跨线程操作无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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