欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# Task Cancel,CancellationTokenSource.IsCancellationRequested

程序员文章站 2022-04-14 16:38:57
public partial class Form1 : Form { CancellationTokenSource cts = new CancellationTokenSource(); public Form1() { InitializeComponent(); } private voi ......
 public partial class form1 : form
    {
        cancellationtokensource cts = new cancellationtokensource();        
        public form1()
        {
            initializecomponent(); 
        }

        private void startclick(object sender, eventargs e)
        {             
            task testtask = task.run(() =>
            {
                while (!cts.iscancellationrequested)
                {
                    string str = $"{datetime.now.tostring("yyyymmddhhmmssffff")},{guid.newguid().tostring()}";
                    begininvoke(new action(() =>
                    {
                        listbox1.items.add(str);
                    }));
                    thread.sleep(500);
                }
            }, cts.token);
        }

        private void cancelclick(object sender, eventargs e)
        {
            cts.cancel();
            string str = "task cancelled";
            listbox1.items.add(str);
        }
    }
public partial class form1 : form
    {
        cancellationtokensource cts = new cancellationtokensource();
        list<string> lblist = new list<string>();      
        public form1()
        {
            initializecomponent();
            listbox1.datasource = lblist; 
        }

        private void startclick(object sender, eventargs e)
        {             
            task testtask = task.run(() =>
            {
                while (!cts.iscancellationrequested)
                {
                    string str = $"{datetime.now.tostring("yyyymmddhhmmssffff")},{guid.newguid().tostring()}";
                    begininvoke(new action(() =>
                    {
                        lblist.add(str);
                        listbox1.datasource = null;
                        listbox1.datasource = lblist;                                         
                    }));
                    thread.sleep(500);
                }
            }, cts.token);
        }

        private void cancelclick(object sender, eventargs e)
        {
            cts.cancel();
            string str = "task cancelled";
            begininvoke(new action(() =>
            {
                lblist.add(str);
                listbox1.datasource = null;
                listbox1.datasource = lblist;                                           
            }));
        }
    }

 

 public partial class form1 : form
    {
        cancellationtokensource cts = new cancellationtokensource();
        list<string> lblist = new list<string>();      
        public form1()
        {
            initializecomponent();
            listbox1.datasource = lblist; 
        }

        private void startclick(object sender, eventargs e)
        {             
            task testtask = task.run(() =>
            {
                while (!cts.iscancellationrequested)
                {
                    addcontenttolbx();                    
                }
            }, cts.token);
        }

        int i = 0;

        private void addcontenttolbx()
        {
            for(i=0;i<10000;i++)
            {
                string str = $"i={i},{datetime.now.tostring("yyyymmddhhmmssffff")},{guid.newguid().tostring()}";
                begininvoke(new action(() =>
                {
                    lblist.add(str);
                    listbox1.datasource = null;
                    listbox1.datasource = lblist;
                }));
                thread.sleep(500);
                if (cts.iscancellationrequested)
                {
                    break;
                }
            }
        }

        private void cancelclick(object sender, eventargs e)
        {
            cts.cancel();
            string str = $"i={i},task cancelled";
            begininvoke(new action(() =>
            {
                lblist.add(str);
                listbox1.datasource = null;
                listbox1.datasource = lblist;                                           
            }));
        }
    }

 

int i = 0;
        list<string> lblist = new list<string>();      
        private bool iscancelled { get; set; }
        public form1()
        {
            initializecomponent();
            listbox1.datasource = lblist; 
        }

        //start or restart
        private void startclick(object sender, eventargs e)
        {
            iscancelled = false;
            if(!iscancelled)
            {
                task testtask = task.run(() =>
                {
                    addcontenttolbx();
                });
            }           
        }        

        private void addcontenttolbx()
        {
            while (i < 10000)
            {
                if (!iscancelled)
                {
                    string str = $"i={i},{datetime.now.tostring("yyyymmddhhmmssffff")},{guid.newguid().tostring()}";
                    begininvoke(new action(() =>
                    {
                        lblist.add(str);
                        listbox1.datasource = null;
                        listbox1.datasource = lblist;
                    }));
                    thread.sleep(500);
                }
                else
                {
                    break;
                }
                i++;
            }
        }

        private void cancelclick(object sender, eventargs e)
        {            
            iscancelled = true;
            string str = $"i={i},task cancelled";
            begininvoke(new action(() =>
            {
                lblist.add(str);
                listbox1.datasource = null;
                listbox1.datasource = lblist;                                           
            }));
        }