C# 线程

来源:互联网 发布:电视机屏幕测试软件 编辑:程序博客网 时间:2024/06/08 09:09

1>引用

using System.Threading;using System.Threading.Tasks;

2> 方法1 Export_Ex()

    public void Export_Ex()        {            try            {                this.m_TokenCancel = new CancellationTokenSource();                this.m_Task = new Task(() => this.exportProcess(this.m_TokenCancel.Token), this.m_TokenCancel.Token);                this.m_Task.Start();                //this.m_Task.Wait();            }            catch (Exception)            {            }        }

3>方法2 exportProcess(CancellationToken token)

   private void exportProcess(CancellationToken token)        {            try            {                if (null == token || true == token.IsCancellationRequested)                {                    throw new OperationCanceledException(token);                }                else                {                        //@! 业务代码 线程里要跑的代码                }            }            catch(Exception )            {            }        }

4>调用方法 btnTest_Click(object sender, EventArgs e)

    private void btnTest_Click(object sender, EventArgs e)        {            this.Export_Ex();        }
0 0