From 70368ab9388e66cb8f8f9965d03a803d29e79207 Mon Sep 17 00:00:00 2001 From: Adam Thorn <alt36@cam.ac.uk> Date: Wed, 10 Jun 2020 17:22:47 +0100 Subject: [PATCH] Add WpkgInstaller.RunWpkgSync() method --- WpkgInstaller/WpkgInstaller.cs | 51 ++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/WpkgInstaller/WpkgInstaller.cs b/WpkgInstaller/WpkgInstaller.cs index 0fe6783..2493a87 100755 --- a/WpkgInstaller/WpkgInstaller.cs +++ b/WpkgInstaller/WpkgInstaller.cs @@ -2,6 +2,7 @@ using System.ServiceProcess; using System.Diagnostics; using System; +using System.Threading; namespace WpkgInstaller { @@ -81,6 +82,56 @@ namespace WpkgInstaller eventLog.WriteEntry("Service stopped", EventLogEntryType.Information); } + private void RunWpkgSync() + { + eventLog.WriteEntry("Starting sync", EventLogEntryType.Information); + + Thread thWorker = new Thread(new ThreadStart(delegate + { + Process process = new Process(); + ProcessStartInfo startInfo = new ProcessStartInfo() + { + RedirectStandardInput = true, + RedirectStandardOutput = true, + RedirectStandardError = true, + WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden, + + FileName = Environment.GetEnvironmentVariable("comspec"), + Arguments = "/C \\\\wpkg.ch.cam.ac.uk\\wpkg\\wpkg.cmd /synchronize", + WorkingDirectory = Environment.GetEnvironmentVariable("temp"), + + UseShellExecute = false, + }; + + + process.StartInfo = startInfo; + + process.EnableRaisingEvents = true; + process.OutputDataReceived += WriteDataToLog; + process.ErrorDataReceived += WriteDataToLog; + + process.Start(); + process.BeginErrorReadLine(); + process.BeginOutputReadLine(); + process.WaitForExit(); + + String msg = String.Format("sync done with exit code {0}", process.ExitCode); + eventLog.WriteEntry(msg, EventLogEntryType.Information); + })); + + thWorker.Start(); + + } + + private void WriteDataToLog(object sendingProcess, DataReceivedEventArgs e) + { + if (e != null) + { + eventLog.WriteEntry(e.Data, EventLogEntryType.Information); + } + + } + private void eventLog_EntryWritten(object sender, System.Diagnostics.EntryWrittenEventArgs e) { -- GitLab