我一直无法让下面的C#线程代码正常运行。我通过单击表单上的按钮来启动一个线程,线程只是声明一个浮点x
并在循环中递增它,直到threadRunning
设置为false。当我再次单击按钮时,我将threadRunning
设置为false。循环完成后,线程将threadResult
设置为递增值x
。然后我在表单上显示threadResult
。
问题是threadResult
在这种情况下总是设置为256.0。如果我选择x
增加0.0001f
,则threadResult
将始终设置为2048.0。每次线程运行时,这些固定值都会分配给threadResult
.. 除非我在递增的行x += 0.00001f;
上放置一个断点。这可以是条件断点,条件设置为false;只要此行上存在已启用的断点,x
就会正确递增并分配给threadResult
。
---编辑1 ---
为了阐明什么是正确的行为,只有在递增的行上存在启用的断点时才会发生:threadResult
被赋予一个等于增量行被命中的次数乘以增量值的值。因此,如果x
增加了121次,增量值为0.00001f,threadResult
应该变为0.00121。如果在递增的行上存在启用的断点并且我在调试模式下运行,则会发生 。
另外,当我在发布模式下运行时,程序会在我单击按钮停止线程时挂起,因此我假设我的线程实现完全出错了。我仍然会欣赏有关我的代码可能出现问题的任何指示。我只是想写一些东西来测试停止一个线程。此外,我不想使用BackgroundWorkers,因为我的线程代码不适用于Windows窗体。 ---结束编辑1 ---
任何人都可以向我解释这里发生了什么吗?通过在线程外部将线程的循环控制变量设置为false,我用来停止线程的方法是否正常?我在Visual Studio 2017,2015和2013中尝试过这些代码,结果相同。
这是代码。表单只包含一个名为button1
的按钮和一个名为textBox1
的文本框。
using System;
using System.Windows.Forms;
using System.Threading;
namespace ThreadingTesting
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Click += Button1_Click;
}
private Thread thread;
private bool threadRunning = false;
private float threadResult = 0;
private void Button1_Click(object sender, EventArgs e)
{
if (threadRunning)
{
threadRunning = false;
thread.Join();
textBox1.Text = "Result: " + threadResult.ToString("N4");
button1.Text = "Start Thread";
}
else
{
thread = new Thread(new ThreadStart(ThreadedWork));
thread.Start();
textBox1.Text = "Thread Started!";
button1.Text = "Stop Thread";
}
}
private void ThreadedWork()
{
threadRunning = true;
float x = 0.0f;
while (threadRunning)
{
x += 0.00001f;
}
threadResult = x;
return;
}
}
}
答案 0 :(得分:3)
256 + 0.00001f
不幸导致256
。这就是它永远不会超过256的原因。
考虑使用double
(d
)而不是float
(f
)。
答案 1 :(得分:-1)
答案就在这里
Why Thread.Join() DOES NOT hang my application when called on UI thread?
在UI线程中调用Button1_Click方法,因此其他线程将无法正常运行。
我认为你必须接近Task或BackgroundWorker ......