简体   繁体   English

无法从线程C#启动另一个进程

[英]can't starting another process from thread c#

I'm trying to make an update app for wpf app. 我正在尝试为wpf应用制作一个更新应用。 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). 但是,当我尝试从更新线程启动另一个进程(Process.Start(path))时,出现了异常(调用线程无法访问此对象,因为另一个线程拥有它)。

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? 如何从另一个线程启动另一个应用程序?

you cannot update UI from another thread, as UI runs on MainThread. 您无法从其他线程更新UI,因为UI在MainThread上运行。 You are using MessageBox.Show() that needs to be displayed on UI. 您正在使用需要在UI上显示的MessageBox.Show()。 And also you cannot retreive UI controls values when on another thread. 而且,当您在另一个线程上时,也无法获取UI控件的值。 In this case use a Dispatcher to update the UI and read info from UI controls. 在这种情况下,请使用Dispatcher来更新UI并从UI控件中读取信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM