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

该对象当前正在其它地方使用
我要做一个WinForm程序,当我单击列表中的某一条记录的时候,WinForm自动加载显示与该条记录相关的数据信息,为了有更好的用户体验,我在WinForm程序加载相关数据的时候显示等待对话框,过程序相关代码如下:

C# code


public void FillSbjForStudent()
{
            if (bASStudentBindingSource.Current != null)
            {
                DataRowView dr = (DataRowView)this.bASStudentBindingSource.Current;
                BASStudentManager manger = new BASStudentManager();
                ThreadStart starter = delegate
              {
                  manger.FillBASSbjForStudent(this.dsSbjForStudent.BASSbjForStudent, new Guid(dr["StudentID"].ToString()));
              };
                loading.Loading(starter);
            }
}

public class BASLoading
    {
        public static bool IsOpen = false;

        public void Loading(ThreadStart starter)
        {
            #region 加载数据动态效果
            try
            {
                TBI.BaseUserControl.FrmLoading frm = new FrmLoading();
                IsOpen = false;                

                starter += delegate {
                    while (true)
                    {
                        
                        if (IsOpen)
                        {
                            frm.Close();
                            break;
                        }
                        
                    }
                };
                Thread th = new Thread(starter);
                th.Start();
                frm.ShowDialog();
            }
            catch(Exception ex)
            {
                
            }
            #endregion
        }
    }




运行的时候,偶尔会出现如题目所示的错误提示

各位大牛,这个问题该怎么解决呢?

------解决方案--------------------
对象没有关闭
------解决方案--------------------
starter中做什么了,报错时出现在哪里,线程中有没有加锁,造成访问冲突
------解决方案--------------------
声明一个静态变量:
private static readonly object lockder = new object();

starter += delegate {
lock(locker)
{while (true)
{

if (IsOpen)
{
frm.Close();
break;
}

}
}
};