前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C#笔记:线程的几种应用

C#笔记:线程的几种应用

作者头像
超级大猪
发布2019-11-22 09:47:02
3160
发布2019-11-22 09:47:02
举报
文章被收录于专栏:大猪的笔记

利用闲暇时间在UI线程的空闲做一些不占时间的操作(不另起线程)和利用委托新建线程实现。   

代码语言:javascript
复制
public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private bool continueCacu = false;
        public delegate void MyCacuDelegate(int a);
        public delegate void MyCacuDelegate2();
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (continueCacu)
            {
                continueCacu = false;
                this.StartOrStop.Content = "Resume";
            }
            else
            {
                continueCacu = true;
                this.StartOrStop.Content = "stop";
                StartOrStop.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.SystemIdle, new MyCacuDelegate(this.CheckNextNumber), 3);
                //MyCacuDelegate dele = new MyCacuDelegate(this.CheckNextNumber);
                //dele.BeginInvoke(3, null, null);
                //CheckNextNumber2();
                //MyCacuDelegate2 dele = new MyCacuDelegate2(CheckNextNumber2);
                //dele.BeginInvoke(null, null);
            }
        }
        public void CheckNextNumber(int num)
        {
            // Reset flag.
            bool isPrime = true;
            for (long i = 3; i <= Math.Sqrt(num); i++)
            {
                if (num % i == 0)
                {
                    // Set not a prime flag to ture.
                    isPrime = false;
                    break;
                }
            }           // If a prime number.
            if (isPrime)
            {
                //StartOrStop.Content = num; 
                //Thread.Sleep(1000);
                this.StartOrStop.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new MyCacuDelegate((x) =>
                {
                    this.StartOrStop.Content = x;
                }), num);
            }
            if (continueCacu)
            {
                StartOrStop.Dispatcher.BeginInvoke(
                    System.Windows.Threading.DispatcherPriority.SystemIdle,
                    new MyCacuDelegate(this.CheckNextNumber), num + 2);
                //MyCacuDelegate dele = new MyCacuDelegate(this.CheckNextNumber);
                //dele.BeginInvoke(num + 2, null, null);
            }
        }
        public void CheckNextNumber2()
        {
            int num = 3;
            while (continueCacu)
            {
                bool isPrime = true;
                for (long i = 3; i <= Math.Sqrt(num); i++)
                {
                    if (num % i == 0)
                    {
                        // Set not a prime flag to ture.
                        isPrime = false;
                        break;
                    }
                }           // If a prime number.
                if (isPrime)
                {
                    //StartOrStop.Content = num; 
                    Thread.Sleep(100);
                    this.StartOrStop.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new MyCacuDelegate((x) =>
                    {
                        this.StartOrStop.Content = x;
                    }
                    ), num);
                }
                num += 2;
            }
        }
    }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015-02-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档