private void OpenFileDialog_FileOk(object sender, CancelEventArgs e)
{
//显示处理中状态
FormState(0); //线程处理导入文件
Thread OpenFileThread = new Thread(new ThreadStart(OpenFile_Thread));
OpenFileThread.IsBackground = true;
OpenFileThread.Start();
}private void OpenFile_Thread()
{
//状态代理
OpenFile_Delegate_State g = new OpenFile_Delegate_State(FormState);
g.Invoke(1);
}private delegate void OpenFile_Delegate_State(int state);
private void FormState(int state)
{
if (state == -1)
{
Btn_Import.Top = Btn_Close.Top = 13;
this.Width = this.FormWidth;
this.Height = 200;//this.FormHeight_Default; 一运行到这里就提示“线程间操作无效,不是从创建‘Form1’的线程访问它”
} if (state == 0)//处理中
{
Panel_Loading.Visible = true;
Panel_Loading.Top = 8;
Panel_Confirm.Visible = false; Btn_Import.Top = Btn_Close.Top = 13;
this.Width = this.FormWidth;
this.Height = this.FormHeight_Default + Panel_Loading.Height + 8;
} if (state == 1)//处理完毕
{
Panel_Loading.Visible = false;
Panel_Confirm.Visible = true; Btn_Import.Top = Btn_Close.Top = 190;
this.Width = this.FormWidth;
this.Height = this.FormHeight_Default - Panel_Loading.Height - 8;
}
}麻烦帮忙看看,一运行修改高度的地方就出错,宽度那个还没事儿。谢谢了~

解决方案 »

  1.   

    使用委托操作相关控件
    Control.CheckForIllegalCrossThreadCalls = false
    Invoke操作 
      

  2.   


    upthis 这个对象当不是创建它的主线程操作是不安全的.
    对此对象的操作可声明下,如:if (this.InvokeRequired){
      Action<int> dlg = (a)=>{
        this.Invoke(dlg,new object[]{state});
      };
    }
    else{
                   this.Width = this.FormWidth;
                    this.Height = 200; 
    }
      

  3.   

    dlg 传递你当前操作this对象的方法,上面是随手的示例
      

  4.   

    Action<int> dlg = (a)=>这个没明白是什么意思!能麻烦告诉我一下这是什么意思吗?谢谢啊~~
      

  5.   

    一个委托,简写方式(lambda),你可以自己定义一个委托(代理)吧,和你:private delegate void OpenFile_Delegate_State(int state);
    一样的道理.delegate void FormStateHandler(int status);
    private void FormState(int state)
    {
      switch(state){
        case -1:
          if(this.InvokeRequired){
           FormStatehandler dlg = new FormStateHandler(FormState);
           this.Invoke(dlg,new object[]{state});
          }
          else{
            Btn_Import.Top = Btn_Close.Top = 13;
                    this.Width = this.FormWidth;
                    this.Height = 200;//
          }
          break;
    ///.....
      }
    }
      

  6.   

    非GUI线程不能直接访问UI控件,需要使用UI控件的invoke方法