I'm trying to make an update app for wpf app. I have an "Update" Button and a little round loader. After update button click, loader appears. Update code uses another thread. But when I'm trying to start another process (Process.Start(path)) from update thread, i got an exeption (the calling thread cannot access this object because a different thread owns it).
private void CheckUpdateButton_OnClick(object sender, RoutedEventArgs e)
{
VisibleLoadingForCheckUpdate = true;
var myThread = new Thread(CheckNeedUpdateApplication);
myThread.Start();
myThread.Name = "UpdateThread";
IsEnabled = false;
}
private void CheckNeedUpdateApplication()
{
if (_baseWindow.ProgramWorked)
{
return;
}
try
{
var myDelegate = new Action(LoaderStop);
if (NeedUpdate())
{
MessageBoxResult updateDecision =
MessageBox.Show(@"Install update?",
@"Update", MessageBoxButton.YesNo);
if (updateDecision == MessageBoxResult.Yes)
InstallUpdate();
Dispatcher.Invoke(myDelegate);
}
else
{
MessageBox.Show(@"No update found", @"Update", MessageBoxButton.OK, MessageBoxImage.Information);
Dispatcher.Invoke(myDelegate);
}
}
catch (Exception)
{
MessageBox.Show(this, @"Connection error",
@"Error", MessageBoxButton.OK, MessageBoxImage.Error);
var myDelegate = new Action(LoaderStop);
Dispatcher.Invoke(myDelegate);
//throw new Exception("Error");
}
}
void LoaderStop()
{
VisibleLoadingForCheckUpdate = false;
IsEnabled = true;
}
private void InstallUpdate()
{
try
{
var newApp = _updateServiceApiClient.GetProgramArchive(_baseWindow.ProgramIdentity);
if (newApp.IsSuccess)
{
using (var decompress = ZipFile.Read(new MemoryStream(newApp.Result)))
{
decompress.ExtractAll(Application.StartupPath + Path.DirectorySeparatorChar + "update", ExtractExistingFileAction.OverwriteSilently);
}
try
{
var pInfo = new ProcessStartInfo(UpdaterFilePath) {UseShellExecute = false};
var mProcess = new Process {StartInfo = pInfo};
mProcess.Start(); //EXCEPTION
_baseWindow.Close();
Close();
}
catch
{
MessageBox.Show(this, @"Update Error.",
@"Error", MessageBoxButton.OK, MessageBoxImage.Error);
LoggerHelper.ErrorsLogger("Update error.");
}
}
}
catch (Exception ex)
{
MessageBox.Show(this, @"Update Error",
@"Error", MessageBoxButton.OK, MessageBoxImage.Error);
//throw new Exception("Ошибка при обновлении.");
}
}
How I can start another app from another thread?