Low performance, system-wide mutex objects pool.
コード例 #1
0
        private static IpcParamEx LoadIpcInfoFile(int nId, bool bOneInstance)
        {
            string strPath = GetIpcFilePath(nId);

            if (string.IsNullOrEmpty(strPath))
            {
                return(null);
            }

            string strMtxName = null;

            if (bOneInstance)
            {
                strMtxName = IpcMsgFilePreID + nId.ToString();
                if (!GlobalMutexPool.CreateMutex(strMtxName, true))
                {
                    return(null);
                }
            }

            IpcParamEx ipcParam = null;

            try
            {
                byte[] pbEnc = File.ReadAllBytes(strPath);
                byte[] pbCmp = CryptoUtil.UnprotectData(pbEnc, IpcOptEnt,
                                                        DataProtectionScope.CurrentUser);
                byte[] pb = MemUtil.Decompress(pbCmp);

                using (MemoryStream ms = new MemoryStream(pb, false))
                {
                    ipcParam = XmlUtilEx.Deserialize <IpcParamEx>(ms);
                }
            }
            catch (Exception) { Debug.Assert(!File.Exists(strPath)); }

            if (bOneInstance)
            {
                RemoveIpcInfoFile(nId);
                if (!GlobalMutexPool.ReleaseMutex(strMtxName))
                {
                    Debug.Assert(false);
                }
            }

            return(ipcParam);
        }
コード例 #2
0
ファイル: IpcUtilEx.cs プロジェクト: zforks/KeeThief
        private static IpcParamEx LoadIpcInfoFile(int nId)
        {
            string strPath = GetIpcFilePath(nId);

            if (string.IsNullOrEmpty(strPath))
            {
                return(null);
            }

            string strMtxName = (IpcMsgFilePreID + nId.ToString());
            // Mutex m = Program.TrySingleInstanceLock(strMtxName, true);
            bool bMutex = GlobalMutexPool.CreateMutex(strMtxName, true);

            // if(m == null) return null;
            if (!bMutex)
            {
                return(null);
            }

            IpcParamEx ipcParam = null;

            try
            {
                XmlSerializer xml = new XmlSerializer(typeof(IpcParamEx));
                FileStream    fs  = new FileStream(strPath, FileMode.Open,
                                                   FileAccess.Read, FileShare.Read);

                try { ipcParam = (IpcParamEx)xml.Deserialize(fs); }
                catch (Exception) { Debug.Assert(false); }

                fs.Close();
            }
            catch (Exception) { }

            RemoveIpcInfoFile(nId);

            // Program.DestroyMutex(m, true);
            if (!GlobalMutexPool.ReleaseMutex(strMtxName))
            {
                Debug.Assert(false);
            }
            return(ipcParam);
        }