C#常用多线程(线程同步,事件触发,信号量,互斥锁,共享内存,消息队列)

复制代码

using System;
using System.Threading;
using System.Windows.Forms;
using UtilForm.Util;

namespace UtilForm
{
    // 线程同步,事件触发,信号量,互斥锁,共享内存,消息队列
    public partial class frmUIThread : Form
    { 
        SynchronizationContext context;// 子线程同步到主线程 
       
        public delegate void DoSth(object sender); // 事件触发
        public event DoSth OnMyEvent;

        Mutex mutex = new Mutex(); // 互斥锁,锁住某一资源 
        ManualResetEvent manualReset = new ManualResetEvent(false); // 信号量,流程上锁定,A执行完再执行B  

        public frmUIThread()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        { 
            context = SynchronizationContext.Current; 
            OnMyEvent += new DoSth(ProgressEvent);
            Console.WriteLine("主线程id:" + Thread.CurrentThread.ManagedThreadId);

            // 线程同步
            ThreadPool.QueueUserWorkItem(state =>
            {
                Console.WriteLine("子线程id:" + Thread.CurrentThread.ManagedThreadId);

                for (int i = 0; i < 3; ++i)
                {
                    //textBox1.Text = i.ToString();//:“线程间操作无效: 从不是创建控件“textBox1”的线程访问它。”
                    //
                    // 写法1:
                    //textBox1.BeginInvoke((MethodInvoker)delegate
                    //{
                    //    textBox1.Text = i.ToString();
                    //});
                    //
                    //
                    // 写法2:
                    context.Send(ProgressUI, i);

                    if (2 == i)
                    {
                        OnMyEvent(i);// 线程间回调
                    }
                    Thread.Sleep(100);
                }
            });

            // 模拟互斥量
            ThreadPool.QueueUserWorkItem(new WaitCallback(ThdMutex));
            ThreadPool.QueueUserWorkItem(new WaitCallback(ThdMutex));

            // 模拟信号量在B流程中阻塞,A流程结束,继续B流程
            ThreadPool.QueueUserWorkItem(state =>
            {
                Console.WriteLine("模拟信号量,A开始执行");
                Thread.Sleep(5000);
                manualReset.Set();// 释放
                Console.WriteLine("模拟信号量,A执行完成");
            });
            ThreadPool.QueueUserWorkItem(state =>
            {
                Console.WriteLine("模拟信号量,B等待执行");
                manualReset.Reset();// 重置信号量 
                manualReset.WaitOne();// 停止
                //manualReset.WaitOne(2000);// 带超时停止
                Console.WriteLine("模拟信号量,B执行完成");
            }); 

            // 共享内存 
            ThreadPool.QueueUserWorkItem(state =>
            {
                MemoryCacheHelper.SetCache("10001", "123");
            });
            ThreadPool.QueueUserWorkItem(state =>
            {
                Thread.Sleep(5000);
                var val = MemoryCacheHelper.GetCache("10001");
                Console.WriteLine($"共享内存:{val}");
            });
        }

        /// <summary>
        /// 更新UI
        /// </summary>
        /// <param name="obj"></param>
        private void ProgressUI(object obj)
        {
            Console.WriteLine("更新UI线程::当前线程id:" + Thread.CurrentThread.ManagedThreadId);

            textBox1.Text = obj.ToString();
        }

        /// <summary>
        /// 事件处理
        /// </summary>
        private void ProgressEvent(object sender)
        {
            Console.WriteLine($"线程事件触发:{sender.ToString()}");
        }

        void ThdMutex(object obj)
        {
            mutex.WaitOne();

            Console.WriteLine($"互斥线程 {Thread.CurrentThread.ManagedThreadId} 正在执行任务");

            Thread.Sleep(1000);

            Console.WriteLine($"互斥线程 {Thread.CurrentThread.ManagedThreadId} 任务执行完毕");

            mutex.ReleaseMutex(); 
        }
    }
}

复制代码

复制代码

using System;
using System.Runtime.Caching;

namespace UtilForm.Util
{
    public class MemoryCacheHelper
    {
        private static MemoryCache cache = MemoryCache.Default;// MemoryCache ObjectCache

        /// <summary>
        /// 读取缓存
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object GetCache(string key)
        {
            var obj = cache.Get(key);
            //Console.WriteLine($"-读取缓存[{key}]:{JsonConvert.SerializeObject(obj, Formatting.Indented)}");
            return obj;
        }

        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="obj"></param>
        /// <param name="timeout">过期时间,默认7200秒</param>
        public static void SetCache(string key, object obj, int timeout = 7200)
        {
            cache.Set(key, obj, DateTimeOffset.Now.AddSeconds(timeout));
            //Console.WriteLine($"-写入缓存[{key}]:{JsonConvert.SerializeObject(obj, Formatting.Indented)}");
        }

        /// <summary>
        /// 删除缓存
        /// </summary>
        /// <param name="key"></param>
        public static void RemoveCache(string key)
        {
            cache.Remove(key);
            //Console.WriteLine($"-移除缓存[{key}]");
        }
    }
}

复制代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值