110,538
社区成员
发帖
与我相关
我的任务
分享
public void GetQueue()
{
while (true)
{
//做一些逻辑处理
}
}
using System;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Bitmap image;
object lockObject = new object();
public Form1() { InitializeComponent(); }
void button1_Click(object sender, EventArgs e)
{
Task.Run(() =>
{
while (true)
{
lock (lockObject)
{
Console.WriteLine(Environment.TickCount);
}
}
});
}
void button2_Click(object sender, EventArgs e) { Monitor.Enter(lockObject); }
void button3_Click(object sender, EventArgs e) { Monitor.Exit(lockObject); }
}
}
button1是启动线程
button2是停止
button3是继续
public void GetQueue()
{
while(true)
{
while(_current)
{
//做一些逻辑处理
}
}
}
外边的循环是保证你开启的线程不会释放掉,里面的循环是保证你的_current为False的时候不会执行需要执行的代码
public void GetQueue()
{
while (true)
{
if(_current)
{
//做一些逻辑处理
}
else
{
//休息下
}
}
}
bool bValid = true;
public void GetQueue()
{
while (true)
{
if(bValid)
{
//做一些逻辑处理
}
else
{
//此处休息
Thread.Sleep(1);
}
Thread.Sleep(1);
}
}
需要跳过,让bValid = false;
需要继续处理,bValid = true;
private ManualResetEventSlim _event = new ManualResetEventSlim();
// 线程循环
public void GetQueue()
{
while (true)
{
_event.Wait(); //等待event
//做一些逻辑处理
}
}
_event.Set(); //开始/继续执行逻辑
_event.Reset(); //暂停执行逻辑