关闭

Cross-thread InvalidOperationException

319人阅读 评论(0) 收藏 举报

windows not allowed the component be accessed by the thread which doesn't create it.

it will cause error Cross-thread InvalidOperationException

 

1.

 

public Form1() {
    InitializeComponent();
 
    Control.CheckForIllegalCrossThreadCalls = false;
}

2.
delegate

 

 

 

 public partial class Form1 : Form
 {
        public delegate void delegate_boundTreeView();     // delegate
        public delegate_boundTreeView myDelegateBoundTreeView;

 private System.Timers.Timer timer_refresh = new System.Timers.Timer(60000);  // another thread

 Form1()
 {
  boundTreeView ();  // main thread

  myDelegateBoundTreeView = new delegate_boundTreeView(boundTreeView);
  timer_refresh.Elapsed += new ElapsedEventHandler(timer_refresh_Elapsed);
  timer_refresh.Start();
 }

 public void boundTreeView ()
 {
  // edit your windows component treeview or anything else (textbox, combobox)
 }
 
 
 public void timer_refresh_Elapsed(object sender, ElapsedEventArgs e)    
        {
            try
            {
                this.Invoke(this.myDelegateBoundTreeView);

            }
            catch
            {
                timer_refresh.Stop();
                //timer_refresh.Enabled = false;
            }
        }
}

 

control.invoke

 

http://msdn.microsoft.com/zh-cn/library/zyzhdc6b.aspx

http://msdn.microsoft.com/zh-cn/library/system.windows.forms.control.invokerequired.aspx

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:67150次
    • 积分:1407
    • 等级:
    • 排名:千里之外
    • 原创:79篇
    • 转载:14篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论