C#一种简单而有效的进程间同步

Wesley13
• 阅读 456
using System;
using System.Threading;
using System.Threading.Tasks;
namespace InterProcessSynchronization
{
    class InterProcessSync
    {
        static void Main(string[] args)
        {
            string MutexName = "InterProcessSyncName";
            Mutex SyncNamed;     //声明一个已命名的互斥对象
             try
            {
                SyncNamed = Mutex.OpenExisting(MutexName);       //如果此命名互斥对象已存在则请求打开
            }
            catch (WaitHandleCannotBeOpenedException)
            {
                SyncNamed = new Mutex(false, MutexName);         //如果初次运行没有已命名的互斥对象则创建一个
            }
            Task MulTesk = new Task
                (
                    () =>                  //多任务并行计算中的匿名方法,用委托也可以
                    {
                        for (; ; )         //为了效果明显而设计
                        {
                            Console.WriteLine("当前进程等待获取互斥访问权......");
                            SyncNamed.WaitOne();
                            Console.WriteLine("获取互斥访问权,访问资源完毕,按回车释放互斥资料访问权.");
                            Console.ReadLine();
                            SyncNamed.ReleaseMutex();
                            Console.WriteLine("已释放互斥访问权。");
                        }
                    }
                );
            MulTesk.Start();
            MulTesk.Wait();
        }
    }
}

以上程序编译后,请运行两个实例即两个进程。就可以明显的看出进程间的同步的实现。

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Stella981 Stella981
2年前
GitHub 上有哪些适合新手跟进的优质项目?
!(https://oscimg.oschina.net/oscnet/011f28e3bc332010e1442e6c00ed344805d.jpg)点击上方“迈微电子研发社”,选择“星标★”公众号重磅干货,第一时间送达!(https://oscimg.oschina.net/oscnet/cd44ba75f
Stella981 Stella981
2年前
C# CRC16 modbus
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceWaterBubbleCheck{
Easter79 Easter79
2年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Wesley13 Wesley13
2年前
C# 一个数组集合,任意组合,不遗漏,不重复
usingSystem;usingSystem.Collections.Generic;usingSystem.Diagnostics;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;names
Stella981 Stella981
2年前
Mvc+三层(批量添加、删除、修改)
DAL层usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingModel;usingSystem.Data;usingNe
Easter79 Easter79
2年前
System.Threading.Thread的使用及传递参数等总结
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;namespaceConsoleApplication3{classProgram{stati
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
C#后台调用webapi
usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Net;usingSystem.Text;usingSystem.Threading.Tasks;
Wesley13 Wesley13
2年前
C# 生成二维码
 1、添加ThoughtWorks.QRCode.dll引用 2、代码usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Threading.Tasks;usingMicrosof