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


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form5 : Form
        {
            public Form5()
            {
                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(object sender, EventArgs e)
            {
                scheduler = TaskScheduler.FromCurrentSynchronizationContext();
                t = new Thread(new ThreadStart(SetData));
                t.IsBackground = true;
                t.Start();
    
                t1 = new Thread(new ThreadStart(Set1));
                t1.IsBackground = true;
                t1.Start();
    
                t2 = new Thread(new ThreadStart(Set2));
                t2.IsBackground = true;
                t2.Start();
            }
    
            void Set()
            {
                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;
    
    
            }
            void SetData()
            {
                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 + "
    ");
                                textBox1.SelectionStart = textBox1.Text.Length;
                                this.textBox1.ScrollToCaret();
                            }
                        }, CancellationToken.None, TaskCreationOptions.None, scheduler);
                        Thread.Sleep(1000);
                        aa = "第一个测试";
                        if (i == 5)
                        {
    
    
                        }
                    }
                    return;
                }
            }
            void Set1()
            {
                while (true)
                {
               
                    for (int i = 0; i < 10; i++)
                    {
                        Task.Factory.StartNew(() =>
                        {
                            lock (syncstate)
                            {
                                textBox1.Text += ("设置1:" + aa + "   " + i + "
    ");
                                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:完成
    ");
                            textBox1.SelectionStart = textBox1.Text.Length;
                            this.textBox1.ScrollToCaret();
                        }
                    }, CancellationToken.None, TaskCreationOptions.None, scheduler);
                    return;
                }
            }
            void Set2()
            {
                while (!bl)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        Task.Factory.StartNew(() =>
                        {
                            lock (syncstate)
                            {
                                textBox1.Text += ("设置2:" + aa + "   " + i + "
    ");
                                textBox1.SelectionStart = textBox1.Text.Length;
                                this.textBox1.ScrollToCaret();
                            }
                        }, CancellationToken.None, TaskCreationOptions.None, scheduler);
                        aa = "第2个设置" + i;
                        Thread.Sleep(500);
                    }
                }
            }
            private void button1_Click(object sender, EventArgs e)
            {
                bl = true;
            }
    
            public DialogResult ShowMessage(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
            {
                var dr = new DialogResult();
                try
                {
                    var tmp = this.Invoke(new MessageBoxShow(MessageBoxShowF), new object[] { text, caption, buttons, icon });
                    if (tmp != null)
                    {
                        dr = (DialogResult)tmp;
                    }
                }
                catch
                {
    
                }
                return dr;
            }
            delegate DialogResult MessageBoxShow(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon);
            DialogResult MessageBoxShowF(string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
            {
    
                var dr = MessageBox.Show(text, caption, buttons, icon);
                return dr;
            }
    
            private void Form5_FormClosed(object sender, FormClosedEventArgs e)
            {
                t.Abort();
                t.Join();
                
                t1.Abort();
                t1.Join();
                t2.Abort();
                t2.Join();
            }
        }
    }
  • 相关阅读:
    实验任务四
    java语言基础第二讲 课程作业02 总结
    java 计算精度处理
    构建之法阅读笔记02
    周活动总结表
    周进度条
    构建之法阅读笔记01
    四则运算软件需求规格说明书
    四则运算2
    周进度条
  • 原文地址:https://www.cnblogs.com/weifeng123/p/10056876.html
Copyright © 2020-2023  润新知