コード例 #1
0
ファイル: SharedMemory.cs プロジェクト: WildGenie/SharmIPC
        /// <summary>
        /// 
        /// </summary>
        /// <param name="uniqueHandlerName">Can be name of APP, both syncronized processes must use the same name and it must be unique among the OS</param>
        /// <param name="bufferCapacity"></param>
        /// <param name="dataArrived">MsgId of the sender, payload</param>
        public SharedMemory(string uniqueHandlerName, Action<eMsgType, ulong, byte[]> dataArrived, long bufferCapacity = 50000, int maxQueueSizeInBytes = 20000000)
        {
            if (dataArrived == null)
                throw new Exception("tiesky.com.SharmIpc: dataArrived callback can't be empty");

            if (String.IsNullOrEmpty(uniqueHandlerName) || uniqueHandlerName.Length > 200)
                throw new Exception("tiesky.com.SharmIpc: uniqueHandlerName can't be empty or more then 200 symbols");

            if (bufferCapacity < 256)
                bufferCapacity = 256;

            if (bufferCapacity > 1000000)    //max 1MB
                bufferCapacity = 1000000;

            this.uniqueHandlerName = uniqueHandlerName;
            this.bufferCapacity = bufferCapacity;

            try
            {
                mt = new Mutex(true, uniqueHandlerName + "SharmNet_MasterMutex");

                if (mt.WaitOne(500))
                {
                    instanceType = eInstanceType.Master;
                }
                else
                {
                    instanceType = eInstanceType.Slave;
                    if (mt != null)
                    {
                        //mt.ReleaseMutex();
                        mt.Close();
                        mt.Dispose();
                        mt = null;
                    }
                }              
            }
            catch (System.Threading.AbandonedMutexException)
            {
                instanceType = eInstanceType.Master;
            }

            Console.WriteLine("tiesky.com.SharmIpc: " + instanceType + " of " + uniqueHandlerName);

            rwh = new ReaderWriterHandler(this, dataArrived);          
        }
コード例 #2
0
ファイル: SharedMemory.cs プロジェクト: WildGenie/SharmIPC
        /// <summary>
        /// Disposing
        /// </summary>
        public void Dispose()
        {
            try
            {
                if (mt != null)
                {
                    mt.ReleaseMutex();
                    mt.Close();
                    mt.Dispose();
                    mt = null;
                }
            }
            catch{
            }

            if (rwh != null)
            {
                rwh.Dispose();
                rwh = null;
            }

        }