C# start thread include Thread,Task,Async/Await,BackgroundWorker,ThreadPool,Timer,Parallel,Parallel.AsParallel()

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp266
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private int num = 0;

        //Dispatcher.BeginInvoke(new Action(() =>
        //            {
        //    lbx.Items.Add($"{++num},{Guid.NewGuid()}");
        //}));
        public MainWindow()
        {
            InitializeComponent();
            this.WindowState = System.Windows.WindowState.Maximized;
            PLinqDemo();
        }

        void PLinqDemo()
        {
            var nums = Enumerable.Range(1, 10000); 
            Dispatcher.BeginInvoke(new Action(() =>
            {
                foreach (var item in nums.AsParallel())
                {
                    lbx.Items.Add($"{item},{Guid.NewGuid()}");
                }
            }));
        }
        void LazyDemo()
        {
            Lazy<int> lazyNum = new Lazy<int>(() =>
            {
                Thread.Sleep(1000);
                return 100;
            });

            Dispatcher.BeginInvoke(new Action(() =>
            {
                lbx.Items.Add($"{lazyNum.Value},{Guid.NewGuid()}");
            }));
        }

        void ThreadLocalDemo()
        {
            ThreadLocal<int> threadLocal = new ThreadLocal<int>();
            while (++(threadLocal.Value) < 100)
            {
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    lbx.Items.Add($"{threadLocal.Value},{Guid.NewGuid()}");
                }));
            }
        }

        void ParallelDemo()
        {
            var nums = Enumerable.Range(1, 1000);
            Parallel.ForEach(nums, x =>
            {
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    lbx.Items.Add($"{x},{Guid.NewGuid()}");
                }));
                Thread.Sleep(20);
            });
        }

        async void AsyncAwaitDemo()
        {
            await Task.Run(() =>
            {
                while (true)
                {
                    Dispatcher.BeginInvoke(new Action(() =>
                    {
                        lbx.Items.Add($"{++num},{Guid.NewGuid()}");
                    }));
                    Thread.Sleep(200);
                }
            });
        }

        void TaskDemo()
        {
            Task.Run(() =>
            {
                while (true)
                {
                    Dispatcher.BeginInvoke(new Action(() =>
                    {
                        lbx.Items.Add($"{++num},{Guid.NewGuid()}");
                    }));
                    Thread.Sleep(200);
                }
            });
        }


        void ThreadDemo()
        {
            Thread ta = new Thread(() =>
            {
                while (true)
                {
                    Dispatcher.BeginInvoke(new Action(() =>
                    {
                        lbx.Items.Add($"{++num},{Guid.NewGuid()}");
                    }));
                    Thread.Sleep(200);
                }
            });
            ta.Start();
        }

        void SystemTimerTimer()
        {
            System.Timers.Timer tmr = new System.Timers.Timer();
            tmr.Elapsed += Tmr_Elapsed;
            tmr.Start();
        }

        private void Tmr_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            while (true)
            {
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    lbx.Items.Add($"{++num},{Guid.NewGuid()}");
                }));
                Thread.Sleep(200);
            }
        }

        void ThreadPoolQueueDemo()
        {
            ThreadPool.QueueUserWorkItem(x =>
            {
                while (true)
                {
                    Dispatcher.BeginInvoke(new Action(() =>
                    {
                        lbx.Items.Add($"{++num},{Guid.NewGuid()}");
                    }));
                    Thread.Sleep(100);
                }
            });
        }

        void BackgroundWorkerDemo()
        {
            BackgroundWorker bgWorker = new BackgroundWorker();
            bgWorker.DoWork += BgWorker_DoWork;
            bgWorker.RunWorkerAsync();
        }

        private void BgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            while (true)
            {
                Dispatcher.BeginInvoke(new Action(() =>
                            {
                                lbx.Items.Add($"{++num},{Guid.NewGuid()}");
                            }));
                Thread.Sleep(100);
            }
        }
    }
}

 

posted @ 2024-08-22 20:54  FredGrit  阅读(2)  评论(0编辑  收藏  举报