收到错误 - System.InvalidOperationException了未处理 [英] getting error - System.InvalidOperationException was unhandled

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

问题描述

我刚开始学习Windows应用程序开发,我们都被赋予自主学习项目来发展Windows应用程序。我想创建发送电子邮件的应用程序。我创建了一个类 MsgSender.cs 来处理这个问题。当我打电话的主要形式类我收到以下错误




System.InvalidOperationException了未处理的。




错误信息 - >




跨线程操作无效:控制从比它on.`

$ b $中创建的线程以外的线程访问'pictureBox1'


的堆栈跟踪如下:

  System.InvalidOperationException了未处理的
消息=跨线程操作无效:控制'pictureBox1从除线程以外的线程访问它被创造的。
来源= System.Windows.Forms的
堆栈跟踪:在System.Windows.Forms.Control.get_Handle()
在System.Windows.Forms.Control.SetVisibleCore(布尔值
)在System.Windows.Forms.Control.set_Visible(布尔值)
在UltooApp.Form1.sendMethod()在D $ b $ A:\Ultoo Application\UltooApp\UltooApp\Form1.cs:第32行
在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)
在System.Threading.ExecutionContext.Run(ExecutionContext中的ExecutionContext,ContextCallback回调,对象状态,布尔ignoreSyncCtx)
的系统。 Threading.ExecutionContext.Run(ExecutionContext中的ExecutionContext,ContextCallback回调,对象状态)
在System.Threading.ThreadHelper.ThreadStart()
的InnerException:

代码:

 私人无效btnSend_Click(对象发件人,EventArgs的发送) 
{
pictureBox1.Visible = TRUE;
计数++;
lblMsgStatus.Visible = FALSE;
getMsgDetails();
味精= txtMsg.Text;

线程t =新主题(新的ThreadStart(sendMethod));
t.IsBackground = TRUE;
t.Start();
}

无效sendMethod()
{
串lblText =(字符串)MsgSender.sendSMS(于味精的Hotmail的uname,PWD);
pictureBox1.Visible = FALSE;
lblMsgStatus.Visible = TRUE;
lblMsgStatus.Text = lblText +\\\
From:+ +的uname到:+ cmbxNumber.SelectedItem ++计数;
}


解决方案

您可以访问GUI线程您试图访问外部GUI线程是获得异常的原因表单GUI控件。您可以使用MethodInvoker访问GUI线程控制。



 无效sendMethod()
{
MethodInvoker MI = {委托
串lblText =(字符串)MsgSender.sendSMS(于味精的Hotmail的uname,PWD);
pictureBox1.Visible = FALSE;
lblMsgStatus.Visible = TRUE;
lblMsgStatus.Text = lblText +\\\
From:+ +的uname到:+ cmbxNumber.SelectedItem ++计数;
};
如果(InvokeRequired)
this.Invoke(MI);
}


I have just started to learn windows application development, and we have been given self learn project to develop one windows application. I am trying to create the application to send email. I have created a class MsgSender.cs to handle that. When I call that class from main form I am getting the following error

System.InvalidOperationException was unhandled.

Error Message-->

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

The stacktrace is as follows:

System.InvalidOperationException was unhandled
Message=Cross-thread operation not valid: Control 'pictureBox1' 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.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Control.set_Visible(Boolean value)
   at UltooApp.Form1.sendMethod() in D:\Ultoo Application\UltooApp\UltooApp\Form1.cs:line 32
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
InnerException: 

Code:

private void btnSend_Click(object sender, EventArgs e)
    {
        pictureBox1.Visible = true;
        count++;
        lblMsgStatus.Visible = false;
        getMsgDetails();
        msg = txtMsg.Text;

        Thread t = new Thread(new ThreadStart(sendMethod));
        t.IsBackground = true;
        t.Start();
    }

    void sendMethod()
    {
        string lblText = (String)MsgSender.sendSMS(to, msg, "hotmail", uname, pwd);
        pictureBox1.Visible = false;
        lblMsgStatus.Visible = true;
        lblMsgStatus.Text = lblText + "\nFrom: " + uname + " To: " + cmbxNumber.SelectedItem + " " + count;
    }

解决方案

You can access Form GUI controls in GUI thread and you are trying to access outside GUI thread that is the reason for getting exception. You can use MethodInvoker to access controls in the GUI thread.

void sendMethod()
{
    MethodInvoker mi = delegate{
       string lblText = (String)MsgSender.sendSMS(to, msg, "hotmail", uname, pwd);
       pictureBox1.Visible = false;
       lblMsgStatus.Visible = true;
       lblMsgStatus.Text = lblText + "\nFrom: " + uname + " To: " +             cmbxNumber.SelectedItem + " " + count;
   }; 
    if(InvokeRequired)
       this.Invoke(mi);
}

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

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