private static void ConsumeMsgInBG(SDBConnection sdbConnection, MessageHandler msgCharHandler, LastWorkExecutor lastWorker) { string responsedMsg; try { while ((responsedMsg = sdbConnection.ReadData(new byte[1])) != string.Empty && responsedMsg != "\0") { msgCharHandler(responsedMsg); } lastWorker?.Invoke(); sdbConnection.Close(); } catch (ThreadAbortException abortException) { Debug.WriteLine($"Message consumer aborted. {abortException.Message}"); sdbConnection.Close(); } }
/// <summary> /// Send sdb command and get string result of the sdb command. /// </summary> /// <param name="serialNumber">Serial number to specify target device. Refer to the 1st column of "$sdb devices".</param> /// <param name="msgCharHandler">Method to be called when each character is returned by the sdb command. String parameter and void return type is required</param> /// <param name="lastWorker">Method to be called when the sdb command response is over. Empty parameter and void return type is required. You can leave it as null for permanent works such as a dlogutil.</param> /// <param name="protocol">Command type of sdb. ex) appcmd, etc</param> /// <param name="cmd">Command to be executed. ex)install, appinfo etc.</param> /// <param name="args">Command to be executed. ex)App ID, package ID etc.</param> /// <returns>Thread instance running in background</returns> public static Thread RequestToTargetAsync(string serialNumber, MessageHandler msgCharHandler, LastWorkExecutor lastWorker, string protocol, string cmd, string[] args) { SDBConnection sdbConnection = EstablishConnection(serialNumber); if (msgCharHandler == null || sdbConnection == null || !SendMsg(sdbConnection, protocol, cmd, args)) { return(null); } Thread mgsConsumerThread = new Thread(() => ConsumeMsgInBG(sdbConnection, msgCharHandler, lastWorker)); mgsConsumerThread.Start(); return(mgsConsumerThread); }