c#无法访问被处置对象 - 工作线程

时间:2015-09-02 03:02:17

标签: c# multithreading

我是一个相对较新的c#用户,如果我犯了一些菜鸟错误,请原谅我。我已经搜索了此错误的解决方法,但无法找到适合我的实现。

我有一个工作线程,它基本上是循环而布尔isCalibrationActive为真。在工作线程中,我设置了一个回调来访问主线程中的标签,并根据工作线程更改文本。在主线程中的formclosing事件上,我将isCalibrationActive设置为false,这理论上应该停止工作线程中的循环。关闭表单后,我收到错误。

表格结束事件代码:

private void Form2_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (e.CloseReason == CloseReason.UserClosing)
        {
            DialogResult result = MessageBox.Show("The Calibration stage is not yet complete. Are you sure you want to exit?", "Dialog Title", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                isCalibrationActive = false;
                Thread.Sleep(10000);
                return;
            }
            else
            {
                e.Cancel = true;
            }
        }
        else
        {
            e.Cancel = true;
        }
    }

工作人员主题:编辑:抱歉,我之前已将测试代码留在那里。

    private void refreshXY()
    {
        var currentpath = new StringBuilder(255);

        //store current directory and set to dll directory.
        UnsafeNativeMethods.GetDllDirectory(currentpath.Length, currentpath);
        UnsafeNativeMethods.SetDllDirectory( "D:\\UI_still_in_progress\\Debug" );
        UnsafeNativeMethods.LoadLibrary("dllTESTER.dll");

        while (isCalibrationActive)
        {                
            xx = UnsafeNativeMethods.grabx();
            yy = UnsafeNativeMethods.graby();
            if (xx == 0) { xx = 0.001;}
            if (yy == 0) { yy = 0.001;}
            SetXlab(xx.ToString());
            SetYlab(yy.ToString());
        }
        //restore old directory
        UnsafeNativeMethods.SetDllDirectory(currentpath.ToString());
    }

在基本相同的SetXlabSetYlab函数期间发生错误。另外我在另一个线程中拥有它的原因是因为它正在从另一个应用程序读取实时数据,需要不断更新。如下所示:

private void SetYlab(string yval)
    {
        // InvokeRequired required compares the thread ID of the
        // calling thread to the thread ID of the creating thread.
        // If these threads are different, it returns true.
        if (this.yshow.InvokeRequired)
        {
            SetTextCallback yD = new SetTextCallback(SetYlab);
            if (isCalibrationActive) { this.Invoke(yD, new object[] { yval }); }
        }
        else
        {
            this.yshow.Text = yval;
        }
    }

具体而言,行this.Invoke(yD, new object[] { yval }); }是导致错误的原因。我有什么想法可以避免这种情况吗?

1 个答案:

答案 0 :(得分:0)

我可能会使用名为System.Windows.Forms (SWF) Timer的{​​{1}},其中calGuiUpdateTimer功能类似

Tick

然后摆脱 xx = 0; yy = 100; xx = UnsafeNativeMethods.grabx(); yy = UnsafeNativeMethods.graby(); if (xx == 0) { xx = 0.001;} if (yy == 0) { yy = 0.001;} this.xshow.Text = xx.ToString(); this.yshow.Text = yy.ToString(); ,而是将其替换为isCalibrationActive

SWF计时器总是在GUI线程上运行,因此您根本不必担心切换线程;你可以完全摆脱所有那些calGuiUpdateTimer.Enabled垃圾的功能。让你的东西更容易。

也就是说,如果InvokeRequiredgrabx可能是慢速运行的函数,此选项将使您的GUI断断续续,并且SWF计时器将是不可接受的。在这种情况下,请继续使用后台线程解决方案,并查看Avoid calling Invoke when the control is disposed以获取指导(另请参阅https://stackoverflow.com/a/661662/171121以简化回调)。