Exemple #1
0
 /// <summary>
 /// 初始化通知和内存共享文件
 /// </summary>
 private void Init(bool read)
 {
     memoryFile = MemoryMappedFileHelper.CreateMemoryMappedFileObj(processKey + MemoryMappedFileNameTag);
     eventWait  = EventWaitHandleHelper.CreateEventHande(processKey + EventWaitNameTag, false);
     if (read)
     {
         Task.Factory.StartNew(WaitForMessage);
     }
 }
Exemple #2
0
        public static void Start(IList <string> serverAddresses, Action action, bool setAddressToMemory)
        {
            if (serverAddresses == null)
            {
                throw new ArgumentNullException(nameof(serverAddresses));
            }
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            var options = new StartOptions();

            foreach (string serverAddress in serverAddresses)
            {
                options.Urls.Add(serverAddress);
            }
            string serverAddressesJoined = string.Join(",", serverAddresses);

            using (WebApp.Start(options))
            {
                FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(Assembly.GetEntryAssembly().Location);

                MemoryMappedFile memoryMappedFile = null;
                if (setAddressToMemory)
                {
                    int capacity = Encoding.UTF8.GetByteCount(serverAddressesJoined);
                    MemoryMappedFileHelper.Write(serverAddressesJoined, Ruya.Connector.Interfaces.Constants.ServerMemoryMappedFileName, capacity, fileVersionInfo.ProductName, out memoryMappedFile);
                }

                // HARD-CODED constant
                string traceMessage1 = $"{fileVersionInfo.ProductName} running on {serverAddressesJoined}";
                Tracer.Instance.TraceEvent(TraceEventType.Start, 0, traceMessage1);
                if (Environment.UserInteractive)
                {
                    Console.WriteLine(traceMessage1);
                }

                Task.Run(action);

                _autoResetEvent = new AutoResetEvent(false);
                _autoResetEvent.WaitOne();

                // HARD-CODED constant
                string traceMessage2 = $"{fileVersionInfo.ProductName} is no longer running on {serverAddressesJoined}";
                Tracer.Instance.TraceEvent(TraceEventType.Stop, 0, traceMessage2);
                if (Environment.UserInteractive)
                {
                    Console.WriteLine(traceMessage2);
                }

                memoryMappedFile?.Dispose();
            }
        }
Exemple #3
0
 /// <summary>
 /// 是否已经在运行
 /// </summary>
 /// <param name="processKey">唯一标识</param>
 /// <param name="activeWindow">已经运行是否激活之前窗体</param>
 /// <returns></returns>
 public static bool HadRun(string processKey, bool activeWindow = false)
 {
     if (activeWindow)
     {
         var handelStr = MemoryMappedFileHelper.CreateMemoryMappedFileObj(processKey + handelTag).ReadData();
         if (!string.IsNullOrEmpty(handelStr))
         {
             var handel = (IntPtr)int.Parse(handelStr);
             Win32Helper.ShowWindowAsync(handel, SW_SHOWNOMAL);
             Win32Helper.SetForegroundWindow(handel);
         }
     }
     return(HadRun(processKey));
 }
Exemple #4
0
        public void TestMemoryMappedFileHelper()
        {
            string fileName = "MyCustomFileName";

            using var file1 = MemoryMappedFileHelper.CreateMemoryMappedFileWithContent(fileName, "Content 1");

            var content = MemoryMappedFileHelper.ReadContent(fileName);

            Logger.Debug("Content1: " + content);
            Assert.Equal("Content 1", content);

            MemoryMappedFileHelper.UpdateContent(fileName, "Content 2");
            content = MemoryMappedFileHelper.ReadContent(fileName);
            Logger.Debug("Content2: " + content);
            Assert.Equal("Content 2", content);
        }
Exemple #5
0
 /// <summary>
 /// 将当前主窗口 句柄写入到进程内存,方便激活
 /// </summary>
 /// <param name="processKey"></param>
 /// <param name="windowHandel"></param>
 public static void WriteHandel(string processKey, IntPtr windowHandel)
 {
     fileMapped = MemoryMappedFileHelper.CreateMemoryMappedFileObj(processKey + handelTag);
     fileMapped.WriteData(windowHandel.ToString());
 }