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; } }); } }
public void StreamDataExchange_Pipes() { bool success = false; bool completed = false; int size = 200000000; string message = GenerateMessage(size); var client = new System.IO.Pipes.NamedPipeClientStream("TESTPIPE"); var server = new System.IO.Pipes.NamedPipeServerStream("TESTPIPE"); var banchmarkTimer = new System.Diagnostics.Stopwatch(); banchmarkTimer.Start(); var reading = new Task(async delegate() { await client.ConnectAsync(); try { // Data to message format. string receivedMessage = await StreamHandler.StreamReaderAsync <string>(client); // Stoping banchmark. banchmarkTimer.Stop(); // Validate data. success = receivedMessage.Equals(message); completed = true; } catch (Exception ex) { Assert.Fail(ex.Message); return; } }); var writing = new Task(async delegate() { // Wait client connection. await server.WaitForConnectionAsync(); try { // Sending message to stream. await StreamHandler.StreamWriterAsync(server, message); } catch (Exception ex) { Assert.Fail(ex.Message); return; } }); reading.Start(); writing.Start(); while (!completed) { Thread.Sleep(5); } float secondsFromStart = banchmarkTimer.ElapsedMilliseconds / (1000.0f); float sharedMBSize = size / 1000000.0f; float speedMBpS = sharedMBSize / secondsFromStart; Console.WriteLine("Transmission time: " + secondsFromStart + " seconds"); Console.WriteLine("Transmisted: " + sharedMBSize + " MB"); Console.WriteLine("Speed: " + speedMBpS + " MB/s | " + (speedMBpS * 8) + "Mb/s"); client.Dispose(); server.Dispose(); Assert.IsTrue(success); }