java 线程 furtu,c#多线程操作测试(阻塞线程,结束任务)

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();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值