日期:2014-05-17  浏览次数:20892 次

C# 等待界面跨线程调用
C# code
Loading.Wait wait = new Loading.Wait("数据加载中...");

        private void myListView_ItemActivate(object sender, EventArgs e)
        {
            new Action(OpenEmr).BeginInvoke(new AsyncCallback(wait.CloseLoading), null);
            wait.ShowDialog();
        }

        delegate void EmrEvent(object[] pa);

        private void OpenEmr()
        {
            object[] pa = (object[])this.myListView.SelectedItems[0].Tag;

            MRWriterForm writerForm = new MRWriterForm();
            writerForm.WindowState = FormWindowState.Maximized;
            PatientDetail patientdetail = pa[0] as PatientDetail;
            writerForm.Patient = patientdetail;
            //病历已经完成不可以修改
            if (patientdetail.EMRState == (int)EMRState.Complete)
            {
                return;
            }
            //writerForm.ShowDialog(this.ParentForm);
            writerForm.ViewFirstPage();
            writerForm.Show();
        }

        private void allListView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //双击设置患者的管床医师
            if (MessageBox.Show("是否将自己设置为该患者的经治医师?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                return;
            }
            //更新患者的经治医师
            DeptAdmission deptadmission = new DeptAdmission();
            object[] obj = this.allListView.SelectedItems[0].Tag as object[];
            deptadmission = obj[1] as DeptAdmission;
            deptadmission.MangerDoc = LoginInfo.Employee.EmployeeID;
            deptadmission.Update();
        }


这个代码提示线程间操作无效: 从不是创建控件“myListView”的线程访问它。

在这一行 object[] pa = (object[])this.myListView.SelectedItems[0].Tag;

请问如何修改呢 谢谢


------解决方案--------------------
C# code

public delegate void NormalCallBack();

private void myListView_ItemActivate(object sender, EventArgs e)
        {
            new Action([color=#FF0000]OpenEmr_Ex[/color]).BeginInvoke(new AsyncCallback(wait.CloseLoading), null);
            wait.ShowDialog();
        }


public void OpenEmr_Ex()
{
if (this.InvokeRequired)
            {
                NormalCallBack ncb = new NormalCallBack(OpenEmr_Ex);
                this.BeginInvoke(ncb);
            }
            else
            {
OpenEmr();
}
}

------解决方案--------------------
建议楼主看下这篇帖子http://topic.csdn.net/u/20120805/01/58230777-2767-4337-a8f8-a3725e7db4b1.html 原理,代码,评测都有