“按下了开始按钮,开始动作”,模拟设备动作,n个汽缸开始依次动作
“step1:汽缸1动作”,模拟汽缸1动作
“step2:汽缸2动作”,模拟汽缸2动作
“step3:汽缸3动作”,模拟汽缸3动作
。
“step n:汽缸n动作”,模拟汽缸n动作
“设备动作完成,待机中。”
大致是这样的,如下
然后,假设说本人在设备运行过程中某个时刻按下“pause ”,想实现如下效果:
“按下了开始按钮,开始动作
“step1:汽缸1动作”
“step2:汽缸2动作”
“step3:汽缸3动作”
“step3:汽缸4动作”
“step3:汽缸5动作”
“用户按下了“pause”,设备暂停运行”
在上述按下了pause的前提下,某个时候,本人又按下“resume”,上面的textbox又能接着显示:
“用户按下了“resume”,设备继续运行”
“step6:汽缸6动作”
“step7:汽缸7动作”
“step8:汽缸8动作”
。
“step n:汽缸n动作”,模拟汽缸n动作
“设备动作完成,待机中。”
以上,先行谢过了,大神们!
5
10
public class TM { private List<Action> Actions = new List<Action>(); private int Position; private bool PauseFlag; private bool StopFlag; public event Action<string> Alert; public void Init(IEnumerable<Action> acts) { lock (Actions) { Actions.Clear(); foreach (var a in acts) Actions.Add(a); } } public void Start() { PauseFlag = false; StopFlag = false; if (Alert != null) Alert("按下了Start按钮啦。"); Position = 0; GoNext(); } private void GoNext() { ThreadPool.QueueUserWorkItem(h => { Action act; lock (Actions) act = Actions[Position++]; act(); if (Alert != null) Alert("执行:" + act.Method.Name + " 完毕!"); if (Position >= Actions.Count) Alert("全部操作执行结束。"); else if (PauseFlag) Alert("用户中暂停中!"); else if (StopFlag) Alert("用户终止了操作!"); else GoNext(); }); } public void Pause() { PauseFlag = true; } public void Resume() { PauseFlag = false; GoNext(); } public void Stop() { StopFlag = true; } }
在执行委托方法的过程中,由于是在子线程执行的,并不会阻塞 UI 主线程的用户操作,也不会阻塞 UI 去显示 Alert 事件为调用者返回的提示信息。
30
{
public partial class Form1 : Form
{
private Task _t = new Task();
public Form1()
{
InitializeComponent();
_t.SendMessage += _t_SendMessage;
}
void _t_SendMessage(string message)
{
this.Invoke((EventHandler)(delegate
{
txtMessage.Text += message+”\r\n”;
}));
}
private void btnStart_Click(object sender, EventArgs e)
{
bool value = btnStart.Text==”Start”?true:false ;
_t.Go(value);
btnStart.Text = _t.IsStop ? “Start” : “Stop”;
}
private void btnPause_Click(object sender, EventArgs e)
{
_t.Pause();
btnPause.Text = _t.IsPause ? “Pause” : “Continue”;
}
}
public class Machine
{
public string Message { get; set; }
}
public class Task
{
public System.Threading.Thread _run = null;
private bool _pause = false;
private bool _stop = false;
private List<Machine> _machines = null;
private static System.Threading.Mutex mut = new System.Threading.Mutex();
public delegate void SendMessageEventHandle(string message);
public event SendMessageEventHandle SendMessage;
public void Go(bool isStart)
{
if(isStart)
{
_machines = new List<Machine>();
for (int i = 0; i < 10; i++)
{
Machine m = new Machine();
m.Message = “这是第” + i + “个气缸”;
_machines.Add(m);
}
_run = new System.Threading.Thread(Do);
_run.Start();
}
else
{
Stop();
}
}
public void Pause()
{
mut.WaitOne();
_pause = !_pause;
mut.ReleaseMutex();
}
public bool IsStop { get { return _stop; } }
public bool IsPause { get { return _pause; } }
public void Stop()
{
mut.WaitOne();
_stop = !_stop;
mut.ReleaseMutex();
}
public void Do()
{
int index = 0;
while (!_stop)
{
if (!_pause)
{
if (SendMessage!=null)
{
SendMessage(_machines[index].Message);
}
index++;
if (index >= _machines.Count)
{
index = 0;
}
}
System.Threading.Thread.Sleep(100);
}
}
}
}
30
{
string Message { get; set; }
int Sleep { get; set; }
void Cry();
}
public class Machine:IAction
{
public string Message { get; set; }
public int Sleep { get; set; }
public void Cry()
{
//崩溃的感觉
}
}
public class Equipment : IAction
{
public string Message { get; set; }
public int Sleep { get; set; }
public void Cry()
{
//疯了的感觉
}
}
private List<IAction> _machines = null;
_machines = new List<IAction>();
//for (int i = 0; i < 10; i++)
//{
// Machine m = new Machine();
// m.Message = “这是第” + i + “个气缸”;
// _machines.Add(m);
//}
IAction a = new Machine();
_machines.Add(a);
a = new Equipment();
_machines.Add(a);
_run = new System.Threading.Thread(Do);
_run.Start();
13
{
//do something
_machines[index].Cry();
//show something
if (SendMessage!=null)
{
SendMessage(_machines[index].Message);
}
index++;
if (index >= _machines.Count)
{
index = 0;
}
}
12
{
if (start == true)
{
pause = true;
thread.Suspend();
textBox1.AppendText(“用户按下了“pause”按钮,应该停止动作:\r\n\n\n”);
}
}
private void btnResume_Click(object sender, EventArgs e)
{
if (pause == true)
{
pause = false;
resume = true;
thread.Resume();
textBox1.AppendText(“继续动作 :\r\n\n”);
}
}