0

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?

1 Answers1

0

you cannot update UI from another thread, as UI runs on MainThread. You are using MessageBox.Show() that needs to be displayed on UI. And also you cannot retreive UI controls values when on another thread. In this case use a Dispatcher to update the UI and read info from UI controls.

Akansha
  • 933
  • 7
  • 18
  • Is there any way to call process.Start(pathToExe) in another thread, not in MainThread. At this moment I got an exception – Gasfar Muhametdinov Aug 07 '15 at 11:42
  • Problem is not that you are creating a thread from MainThread. Problem is that you are using some UI elements inside your new process thread. Example MessageBox. and what is _baseWindow? – Akansha Aug 07 '15 at 12:02
  • I think problem is in InstallUpdate(). Are you able to see the messageBox??and what is _baseWindow? – Akansha Aug 07 '15 at 12:08
  • MessageBox is showing, and other UI elements are working great. There are no exceptions using UI elements. exception appears when i'm trying to start another program with Process.Start in another thread. not the same program. _baseWindow - another window of the application – Gasfar Muhametdinov Aug 07 '15 at 12:11