如何从线程中获取价值?

发布于 2024-11-08 20:00:26 字数 1498 浏览 0 评论 0原文

嗨,我试图从我的线程中获取一个值,但它似乎对我来说不太有效,当然我发现我的代码结构足够不稳定。这是我的代码,我将我的线程类命名为“clsThreadCount”,下面是我的实现

public volatile bool Grab = false;
public volatile int count = 0;
public void Initialization(int i)
{
   count = i;
}
public void Play() 
{
   Grab = false;
   _shouldStop = false;
   ThreadTest();
}
public void Stop()
{
   _shouldStop = true;
   workerThread.Join(1);
   workerThread.Abort();
}
private void ThreadTest()
{
   workerThread = new Thread(DoWork);
   workerThread.Start();
   while (!workerThread.IsAlive) ;
}
private void DoWork()
{
   try
       {
          while (!_shouldStop)
          {
              if (Grab)
              {
                  count++;
                  Grab = false;
              }
          }
      }
      catch (Exception)
      {
          Play();
      }
      finally
      {
      }
 }

当我的程序(主菜单)开始运行时,我将触发初始化函数,将参数传递为 7

ObjThreadCount.Initialization(7); // count = 7
ObjThreadCount.Play();            // the thread are running
ObjThreadCount.Grab = true;       // the grab equal to true, count++ are trigger
Thread.Sleep(100);                // wait awhile
lblResult.Text = ObjThreadCount.count.ToString();  // sometime i can get count++ result (e.g. 8)
ObjThreadCount.Stop();            // thread stop

,有时我的程序可以从线程中获得正确的计数,但有时却不能。 我意识到在我的 while 循环实现中缺少一些东西.. 像 waitone 或 waitautoevent 这样的东西..我可以忽略 Thread.Sleep(100) 吗?我应该在 while 循环中添加哪些合适的代码? 请帮帮我~:S ** 抱歉,在第一次上传时,我忘记在变量中写下“易失性” 谢谢..

Hi i am trying to grab a value from my threading but it seem work not so find to me course i found that my code structure are unstable enough..here is my code i name my thread class as "clsThreadCount" and below is my implementation

public volatile bool Grab = false;
public volatile int count = 0;
public void Initialization(int i)
{
   count = i;
}
public void Play() 
{
   Grab = false;
   _shouldStop = false;
   ThreadTest();
}
public void Stop()
{
   _shouldStop = true;
   workerThread.Join(1);
   workerThread.Abort();
}
private void ThreadTest()
{
   workerThread = new Thread(DoWork);
   workerThread.Start();
   while (!workerThread.IsAlive) ;
}
private void DoWork()
{
   try
       {
          while (!_shouldStop)
          {
              if (Grab)
              {
                  count++;
                  Grab = false;
              }
          }
      }
      catch (Exception)
      {
          Play();
      }
      finally
      {
      }
 }

when my program(main menu) are starting to run i will trigger the initialize function at pass the parameter as 7

ObjThreadCount.Initialization(7); // count = 7
ObjThreadCount.Play();            // the thread are running
ObjThreadCount.Grab = true;       // the grab equal to true, count++ are trigger
Thread.Sleep(100);                // wait awhile
lblResult.Text = ObjThreadCount.count.ToString();  // sometime i can get count++ result (e.g. 8)
ObjThreadCount.Stop();            // thread stop

sometime my program can able to get a right counting from the thread but sometime are not.
i realize at my while loop implementation there are something are missing..
something like waitone or waitautoevent..can i ignore Thread.Sleep(100) ?? what are the suitable code should i add in the while loop ?
Please help me~ :S
** sorry in the first upload i forgot to write down "volatile" into the variable
thank you..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你是暖光i 2024-11-15 20:00:26

如果是 C#(以及 C 和 java,可能还有 C++),则需要将 _shouldStopGrab 声明为 易失性

If C# (and C and java, and probably C++), you need to declare _shouldStop and Grab as volatile.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文