private static async Task Loop(TcpConnectorSlave connector, AdapterBase module) { Process?parentProcess = null; using (Request request = await connector.ReceiveRequest(5000)) { if (request.Code != AdapterMsg.ID_ParentInfo) { throw new Exception("Missing ParentInfo request"); } ParentInfoMsg?info = StdJson.ObjectFromUtf8Stream <ParentInfoMsg>(request.Payload); if (info == null) { throw new Exception("ParentInfoMsg is null"); } parentProcess = Process.GetProcessById(info.PID); connector.SendResponseSuccess(request.RequestID, s => { }); } Thread t = new Thread(() => { ParentAliveChecker(parentProcess); }); t.IsBackground = true; t.Start(); var helper = new AdapterHelper(module, connector); bool run = true; while (run) { using (Request request = await connector.ReceiveRequest()) { helper.ExecuteAdapterRequestAsync(request); bool shutdown = request.Code == AdapterMsg.ID_Shutdown; run = !shutdown; } } // Wait until parent process kills us (after Shutdown method completed) while (true) { await Task.Delay(1000); } }