黑马程序员技术交流社区

标题: 。关于线程访问的问题 [打印本页]

作者: _xixi_    时间: 2014-7-9 18:29
标题: 。关于线程访问的问题
本帖最后由 _xixi_ 于 2014-7-10 22:44 编辑

我想写个线程,实现秒表功能,可是运行出错了,提示“线程间操作无效: 从不是创建控件“txtSecond”的线程访问它。”
以下是我的代码,请大家不吝赐教
  1. namespace clock
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.         public Form1()
  6.         {
  7.             InitializeComponent();
  8.         }

  9.         private void btnRun_Click(object sender, EventArgs e)
  10.         {
  11.             Thread th = new Thread(Sencond);
  12.             th.Start();
  13.         }

  14.         private void Sencond()
  15.         {
  16.             while (true)
  17.             {
  18.                 Thread.Sleep(100);
  19.                 int s = Convert.ToInt32(txtSecond.Text);
  20.                 if (s == 59)
  21.                 {
  22.                     txtSecond.Text = "0";
  23.                 }
  24.                 else
  25.                 {
  26.                     s += 1;
  27.                     txtSecond.Text = s.ToString();
  28.                 }
  29.             }
  30.             
  31.         }
  32.     }
  33. }
复制代码




作者: 追梦无悔    时间: 2014-7-9 19:55
在构造方法中添加一条:        CheckForIllegalCrossThreadCalls = false;
控件不允许线程间操作 ,有两种方法解决这个问题,一种是:使用委托; 第二种是:CheckForIllegalCrossThreadCalls就是把程序给蒙蔽了,不让其 捕捉线程间错误的操作
   
作者: 周星星同学    时间: 2014-7-9 20:31
还没学。。。。。。
作者: 麦田怪圈    时间: 2014-7-10 18:55
路过,虽然不懂但是觉得表叫牛哦!
作者: _xixi_    时间: 2014-7-10 22:43
追梦无悔 发表于 2014-7-9 19:55
在构造方法中添加一条:        CheckForIllegalCrossThreadCalls = false;
控件不允许线程间操作 ,有两种 ...

谢谢~CheckForIllegalCrossThreadCalls = false;  解决了我的问题
作者: 追梦无悔    时间: 2014-7-11 07:34
_xixi_ 发表于 2014-7-10 22:43
谢谢~CheckForIllegalCrossThreadCalls = false;  解决了我的问题

:) ok  解决了就好~
作者: gwljp    时间: 2014-7-11 14:31
你的是子线程调用主线程控件问题,一般都是使用委托来达到效果的
作者: _xixi_    时间: 2014-7-11 22:42
gwljp 发表于 2014-7-11 14:31
你的是子线程调用主线程控件问题,一般都是使用委托来达到效果的

谢谢你的提点
作者: 小郭zaiheima    时间: 2014-7-12 00:49
有点代码还真的不知道什么意思。。好伟大的样子
作者: _xixi_    时间: 2014-7-12 12:57
本帖最后由 _xixi_ 于 2014-7-14 14:24 编辑

总算把委托的方法给实现了,谢谢各位的指点!
  1. namespace clock
  2. {
  3.     delegate void GetSec();
  4.     public partial class Form1 : Form
  5.     {
  6.         GetSec getSec;
  7.         Thread th;
  8.         public Form1()
  9.         {
  10.             InitializeComponent();
  11.         }

  12.         private void btnRun_Click(object sender, EventArgs e)
  13.         {
  14.             if (th==null||!th.IsAlive)
  15.             {
  16.                 th = new Thread(new ThreadStart(SecondRun));
  17.                 th.Start();
  18.             }
  19.         }

  20.         private void GetSecond()
  21.         {
  22.             if (txtSecond.InvokeRequired == true) //判断是否跨线程访问了
  23.             {
  24.                     txtSecond.Invoke(getSec);   //执行委托,也就是回到主线程执行GetSec()方法
  25.             }
  26.             else
  27.             {  
  28.                 if (th.IsAlive)
  29.                 {
  30.                     int s = Convert.ToInt32(txtSecond.Text);
  31.                     if (s == 59)
  32.                     {
  33.                         txtSecond.Text = "0";
  34.                     }
  35.                     else
  36.                     {
  37.                         s += 1;
  38.                         txtSecond.Text = s.ToString();
  39.                     }
  40.                 }
  41.                 Thread.Sleep(10);
  42.             }
  43.             
  44.         }

  45.         private void SecondRun()
  46.         {
  47.             getSec = new GetSec(GetSecond);
  48.             while (true)
  49.             {
  50.                 getSec();
  51.             }
  52.            
  53.         }

  54.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  55.         {
  56.             if (th.IsAlive)
  57.             {
  58.                 th.Abort();//确保关闭窗口之前,终止线程
  59.             }
  60.         }
  61.     }

  62. }
复制代码




作者: 绿箭    时间: 2014-7-13 20:54
_xixi_ 发表于 2014-7-12 12:57
总算把委托的方法给实现了,谢谢各位的指点!

代码挺牛叉的,借鉴一下呀,研究研究
作者: _xixi_    时间: 2014-7-13 22:23
绿箭 发表于 2014-7-13 20:54
代码挺牛叉的,借鉴一下呀,研究研究

这个在sleep这里还有点问题的,时间过长的话,会有种很卡的感觉,一起学习罗~




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2