I am trying through my console application or windows service to run the batch. Into batch I write commands which run my Setup.msi and create InstallLogFile.
Trying with console application. Problem arises when the Setup.msi is run and pop up me with "The 'MyConsoleApplication' should be closed before continuing the install". And I have option to check Automatically close applications and attempt to restart them after setup is complete.
Trying with starting my created Windows Service. Here is the thing little bit complicated cause after the Setup.msi is run, I don't see any pop up message, just through the log I find that my service is Stopped (probably my Setup.msi requires like in 1. case but I have no idea why) and after installation, service again Started and do the whole logic again, which I want to avoid.
First in OnStart method of the windows service I called Update() in separate thread method which handle whole process.
protected override void OnStart(string[] args)
{
try
{
Task.Run(() =>
{
try
{
Update();
}
catch (Exception ex)
{
}
finally
{
}
});
}
catch (Exception ex)
{
}
}
I am not including whole code, just logic without log information.
In update method some of the core logic is: 1. Download the Setup.msi 2. Close process and stop some services 3. Start process which runs .bat with code which starts Setup.msi installation
public void Update()
{
var downloadLink = @"someLinkOnDropbox";
// try to download
try
{
using (var client = new WebClient())
{
client.DownloadFile(downloadLink, myDirLocation);
}
}
catch (Exception ex)
{
return;
}
while (ProcessIsRunning("someProcess") || ServiceIsRunning("someService1") || ServiceIsRunning("someService2") || ServiceIsRunning("someService3"))
{
// try to terminate process
StopProcess("someProcess");
// try to stop the service
StopService("someService1");
StopService("someService2");
StopService("someService3");
}
// wait 5 sec process and services are stopped
Thread.Sleep(5000);
// path to the my .bat file on D: disc which run Setup.msi (also downloaded on my D: disc)
const string path = @"somePath";
const string fileName = @"batName.bat";
const string workingDirectory = @"myDir";
try
{
Task.Run(() =>
{
StartProcess(path, fileName, workingDirectory);
});
}
catch (Exception ex)
{
}
}
Also, methods StopProcess, StopService, ProcessIsRunning and ServiceIsRunning are custom made and working fine in that point I have no problems.
Method StartProcess is below:
public void StartProcess(string path, string fileName, string workingDirectory)
{
ProcessStartInfo psi = new ProcessStartInfo(path)
{
FileName = fileName,
WorkingDirectory = workingDirectory,
UseShellExecute = true,
CreateNoWindow = false,
WindowStyle = ProcessWindowStyle.Hidden
};
// Try to start the process.
try
{
Process p = new Process {StartInfo = psi};
p.Start();
Log.Instance.Warn($"Process started");
p.WaitForExit();
Log.Instance.Warn($"Process finished");
}
catch (Exception ex)
{
}
}
Code which is in .bat file: msiexec /i Setup.msi /l*v SetupInstall.log INSTALL_SETTINGS_FOR="deviceID"
Here is the thing what makes me problem with my service.
My application which I want to update via MyService are on the same location and used some same files. So during update process when MyService run it through the batch, installation requires to stop service cause service used some files which need to be updated, cause also application uses it.
Solution was to move Service installation location into the new dir. Something like D:\\MyApp for the app, and D:\\MyApp\\MyService for the service, and in this case shared files between MyApp and MyService can be updated in MyApp without requirement from MyApp to stop MyService.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.