【问题标题】:multithreadin on gui in c#c#中gui上的多线程
【发布时间】:2014-06-07 06:17:00
【问题描述】:

我正在开发一个在 GUI 上包含多线程的项目 这是我的代码 在我的 form.cs 文件中,我使用此代码

    delegate void voidstringfunction(System.Drawing.Image x);

    public void ShowImage(System.Drawing.Image x)
    {

        if (pictureBox1.InvokeRequired)
            pictureBox1.Invoke(new voidstringfunction(ShowImage), x);

        pictureBox1.Image = x;
        pictureBox1.Update();

    }

    public void ThreadFunc(){
        SGSserverForm form2 = (SGSserverForm)(Application.OpenForms[0]);
        form2.ShowImage(tempImage);
    }
    //this event is running too
    void videoSource_NewFrameEvent(object sender, AForge.Video.NewFrameEventArgs eventArgs)
    {
        pictureBox5.Invoke(new MethodInvoker(() => pictureBox5.Image = tempIm));
    }

当我启动线程时,在 program.cs 文件中我得到这个异常“对象当前正在其他地方使用。” 包含

的行
sgsClientForm.ShowDialog();

有什么问题? 谢谢大家

【问题讨论】:

  • 把try catch放到你使用过的线程和函数中,尝试定位问题的确切位置。由于您没有提供足够的信息,因此很难找出问题所在。

标签: c# multithreading user-interface


【解决方案1】:

你检查你是否需要调用,如果需要,Invoke。但是,在调用之后,您继续该方法,无论如何都要在“非 GUI”线程上执行操作。试试这个:

public void ShowImage(System.Drawing.Image x)
{
    if (pictureBox1.InvokeRequired)
    {
        pictureBox1.Invoke(new voidstringfunction(ShowImage), x);
        return;
    }

    pictureBox1.Image = x;
    pictureBox1.Update();
}

【讨论】:

  • 对,对不起,我的错。打算删除我的评论,因为它对任何人都没有帮助
猜你喜欢
  • 2011-06-24
  • 1970-01-01
  • 1970-01-01
  • 2015-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-04
  • 1970-01-01
相关资源
最近更新 更多