程序中用到Timer事件↓
private void timer1_Tick(object sender, EventArgs e)
{
     this.lb_name_1.Text = "1";
}
这句运行就报 程间操作无效: 从不是创建控件“lb_name_1”的线程访问它
但是单个拿出来运行就没问题- =我在想是不是程序太繁琐的问题 如果是的话 怎么解决....求解

解决方案 »

  1.   


            public void Output(string message)
            {
                if (null == this.ListView)
                    throw new NullReferenceException("The ListView is null");            WaitCallback waitCallBack = new WaitCallback(this.DoSomethingWithAction);
                ThreadPool.QueueUserWorkItem(waitCallBack, message);
            }        #endregion        public void DoSomethingWithAction(Object x)
            {
                this.ListView.Invoke(new Action<string>(this.ChangeUI), x.ToString());
            }        private void ChangeUI(string message)
            {
                if (this.ListView.Items.Count > 200)
                    this.ListView.Items.Clear();
                this.ListView.Items.Add(message);            this.ListView.Items[this.ListView.Items.Count - 1].Selected = true;
                this.ListView.Items[this.ListView.Items.Count - 1].EnsureVisible();
            }
      

  2.   

    用BeginInvoke或Invoke,
    public delegate void InvokeDelegate();
    private void timer1_Tick(object sender, EventArgs e)
    {
       Invoke(new InvokeDelegate(InvokeMethod));
    }
    public void InvokeMethod()
    {
       this.lb_name_1.Text = "1";
    }
     
      

  3.   

    if (this.InvokeRequired)
                {
                    this.Invoke(new MethodInvoker(delegate { timer1_Tick(sender,e)
    ; }));
                    return;
                }
      

  4.   


    private void timer1_Tick(object sender, EventArgs e)
    {
      if (this.InvokeRequired)
      {
          this.Invoke(new MethodInvoker(delegate { timer1_Tick(sender,e);}));
          return;
      }
      this.lb_name_1.Text = "1";
    }
      

  5.   

    我的问题不是出在Timer时间上去掉事件也一样报错 抱歉 分还是会给 谢谢了
      

  6.   


    方法前面加上这一句就行了
    if (this.InvokeRequired)
    {
       this.Invoke(new MethodInvoker(delegate { timer1_Tick(sender,e); }));
       return;
    }