usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceWindowsFormsApplication1
{public partial classForm5 : Form
{publicForm5()
{
InitializeComponent();
}string aa = "aaaa";bool bl = false;private object syncstate = new object();
TaskScheduler scheduler= null;
System.Timers.Timer tmPulse= new System.Timers.Timer();//位置刷新
System.Timers.Timer tmPulse1 = new System.Timers.Timer();//位置刷新
Thread t;
Thread t1;
Thread t2;private void Form5_Load(objectsender, EventArgs e)
{
scheduler=TaskScheduler.FromCurrentSynchronizationContext();
t= new Thread(newThreadStart(SetData));
t.IsBackground= true;
t.Start();
t1= new Thread(newThreadStart(Set1));
t1.IsBackground= true;
t1.Start();
t2= new Thread(newThreadStart(Set2));
t2.IsBackground= true;
t2.Start();
}voidSet()
{
tmPulse.Interval= 200;
tmPulse.AutoReset= true;//设置是执行一次(false)还是一直执行(true); 位置刷新
tmPulse.Start();
tmPulse.Elapsed+= (o, a) =>{
SetData();
};
tmPulse.Enabled= true;
tmPulse1.Interval= 200;
tmPulse1.AutoReset= true;//设置是执行一次(false)还是一直执行(true); 位置刷新
tmPulse1.Start();
tmPulse1.Elapsed+= (o, a) =>{
Set1();
};
tmPulse1.Enabled= true;
}voidSetData()
{while (true)
{for (int i = 0; i < 10; i++)
{if (i == 2)
{
ShowMessage("执行结果,是否成功?", "结果确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
}
Task.Factory.StartNew(()=>{lock(syncstate)
{
textBox1.Text+= ("测试:" + aa + " " + i + "\r\n");
textBox1.SelectionStart=textBox1.Text.Length;this.textBox1.ScrollToCaret();
}
}, CancellationToken.None, TaskCreationOptions.None, scheduler);
Thread.Sleep(1000);
aa= "第一个测试";if (i == 5)
{
}
}return;
}
}voidSet1()
{while (true)
{for (int i = 0; i < 10; i++)
{
Task.Factory.StartNew(()=>{lock(syncstate)
{
textBox1.Text+= ("设置1:" + aa + " " + i + "\r\n");
textBox1.SelectionStart=textBox1.Text.Length;this.textBox1.ScrollToCaret();
}
}, CancellationToken.None, TaskCreationOptions.None, scheduler);
aa= "第1个设置" +i;
Thread.Sleep(500);
}
Task.Factory.StartNew(()=>{lock(syncstate)
{
textBox1.Text+= ("设置1:完成\r\n");
textBox1.SelectionStart=textBox1.Text.Length;this.textBox1.ScrollToCaret();
}
}, CancellationToken.None, TaskCreationOptions.None, scheduler);return;
}
}voidSet2()
{while (!bl)
{for (int i = 0; i < 10; i++)
{
Task.Factory.StartNew(()=>{lock(syncstate)
{
textBox1.Text+= ("设置2:" + aa + " " + i + "\r\n");
textBox1.SelectionStart=textBox1.Text.Length;this.textBox1.ScrollToCaret();
}
}, CancellationToken.None, TaskCreationOptions.None, scheduler);
aa= "第2个设置" +i;
Thread.Sleep(500);
}
}
}private void button1_Click(objectsender, EventArgs e)
{
bl= true;
}public DialogResult ShowMessage(string text, stringcaption, MessageBoxButtons buttons, MessageBoxIcon icon)
{var dr = newDialogResult();try{var tmp = this.Invoke(new MessageBoxShow(MessageBoxShowF), new object[] { text, caption, buttons, icon });if (tmp != null)
{
dr=(DialogResult)tmp;
}
}catch{
}returndr;
}delegate DialogResult MessageBoxShow(string text, stringcaption, MessageBoxButtons buttons, MessageBoxIcon icon);
DialogResult MessageBoxShowF(string text, stringcaption, MessageBoxButtons buttons, MessageBoxIcon icon)
{var dr =MessageBox.Show(text, caption, buttons, icon);returndr;
}private void Form5_FormClosed(objectsender, FormClosedEventArgs e)
{
t.Abort();
t.Join();
t1.Abort();
t1.Join();
t2.Abort();
t2.Join();
}
}
}