예제 #1
0
		static async Task StartServerActivatorAsync(string npname)
		{
			await Task.Delay(1);
			while (!ApplicationCTS.IsCancellationRequested)
			{
				bool everConnected = false;
				try
				{
					using (var nps = new System.IO.Pipes.NamedPipeServerStream(npname, System.IO.Pipes.PipeDirection.InOut
						, 1, System.IO.Pipes.PipeTransmissionMode.Byte
						, System.IO.Pipes.PipeOptions.Asynchronous | System.IO.Pipes.PipeOptions.WriteThrough, 65536, 65536))
					{
						await nps.WaitForConnectionAsync(ApplicationCTS.Token);
						everConnected = true;
						byte[] msg = System.Text.Encoding.ASCII.GetBytes(CurrentProcess.Id.ToString());
						await nps.WriteAsync(msg);
						await nps.FlushAsync();
						//nps.Disconnect(); //do not disconnect
						nps.Close();
					}
				}
				catch (Exception x)
				{
					if (ApplicationCTS.IsCancellationRequested)
						return;
					WriteDebugLine(x);
					if (!everConnected)
					{
						await Task.Delay(1000);
						continue;
					}
				}
				PostToAppThread(delegate
				{
					WriteDebugLine("ServerActivator");

					var mf = MainBrowser?.FindForm();
					if (mf?.Visible == true)
					{
						ActivateForm(mf);
						return;
					}
					var forms = WF.Application.OpenForms;
					for (var i = 0; i < forms.Count; i++)
					{
						var form = forms[i];
						if (!form.Visible)
							continue;

						//WindowsInterop.SetForegroundWindow(form.Handle);
						ActivateForm(form);
						break;
					}
				});
			}
		}