private void worker_DoWork(object sender, DoWorkEventArgs e) { try { worker.ReportProgress(20, new object[] { "Stopping TermService...", false }); patcher.StopTermService(); worker.ReportProgress(40, new object[] { " Done", true }); if (Convert.ToBoolean(((object[])e.Argument)[0])) { //patch List <object> patch = (List <object>)((object[])e.Argument)[1]; if (patcher.CheckStatus(patch) == Patcher.Status.Unpatched) { worker.ReportProgress(60, new object[] { "Patching termsrv.dll", false }); patcher.Patch(patch); } } else { //unpatch worker.ReportProgress(60, new object[] { "Restoring termsrv.dll backup", false }); patcher.Unpatch(); } worker.ReportProgress(80, new object[] { "Starting TermService...", false }); patcher.StartTermService(); worker.ReportProgress(100, new object[] { " Done", true }); } catch (Exception exception) { worker.ReportProgress(100, new object[] { exception.ToString(), false }); } }
private void worker_DoWork(object sender, DoWorkEventArgs e) { try { worker.ReportProgress(20, new object[] { $"Stopping TermService (might take up to {3 * Patcher.ServiceTimeout} seconds)...", false }); patcher.StopTermService(); worker.ReportProgress(40, new object[] { " Done", true }); if (Convert.ToBoolean(((object[])e.Argument)[0])) { //patch List <object> patch = (List <object>)((object[])e.Argument)[1]; if (patcher.CheckStatus(patch) == Unpatched) { worker.ReportProgress(60, new object[] { "Patching termsrv.dll...", false }); patcher.Patch(patch); worker.ReportProgress(70, new object[] { " Done", true }); } } else { //unpatch worker.ReportProgress(60, new object[] { "Restoring termsrv.dll backup...", false }); patcher.Unpatch(); worker.ReportProgress(70, new object[] { " Done", true }); } worker.ReportProgress(80, new object[] { $"Starting TermService (might take up to {Patcher.ServiceTimeout} seconds)...", false }); try { patcher.StartTermService(); } catch (System.ServiceProcess.TimeoutException) { worker.ReportProgress(100, new object[] { " Failed", true }); worker.ReportProgress(100, new object[] { "Starting TermService timed out", false }); } worker.ReportProgress(100, new object[] { " Done", true }); } catch (ServiceStopException) { // Unable to stop the service worker.ReportProgress(100, new object[] { " Failed", true }); worker.ReportProgress(100, new object[] { "Stop the Remote Desktop service manually and try again", false }); } // HResult = 32 -> ERROR_SHARING_VIOLATION -> File in use by another process catch (System.IO.IOException exception) when((exception.HResult & 0x0000FFFF) == 32) { worker.ReportProgress(100, new object[] { " Failed", true }); worker.ReportProgress(100, new object[] { $"{patcher.TermsrvPath} is in use by another process, stop the Remote Desktop service manually and make sure no process is locking the file and try again", false }); } catch (Exception exception) { worker.ReportProgress(100, new object[] { exception.ToString(), false }); } }