等待一个函数调用将完成 c#

wait for one function call would finish c#

在我的应用程序中有两个按钮:

void Install_1_Click(object sender , RoutedEventArgs e)
{
 RunSilentSetup("C:\app1.exe");
}
.
.
.
void Install_2_Click(object sender , RoutedEventArgs e)
{
 RunSilentSetup("C:\app2.exe");
}

以及运行设置的方法:

void RunSilentSetup(string executableFilePath)
{
 //code that runs the setup goes here
}

问题:如果用户单击一个按钮并立即按下第二个按钮,则会抛出无法同时安装的错误。

我的问题:我希望第二次调用方法 RunSilentSetup 会等到第一次调用完成,我该如何实现?

编辑安装代码:

   try
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.CreateNoWindow = false;
            startInfo.UseShellExecute = true;
            startInfo.FileName = executableFilePath;
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            startInfo.Arguments = "/s /v/qn";
            using (Process exeProcess = Process.Start(startInfo))
            {
                exeProcess.WaitForExit();
                int exitcode = exeProcess.ExitCode;

                if (exitcode == 0)
                    this.Dispatcher.Invoke(() =>
                    {
                        ShowMessage(_vm.ShowSuccess , "Installation was successfully completed");
                    });
                else
                    this.Dispatcher.Invoke(() =>
                    {
                        ShowMessage(_vm.ShowError , $"one or more errors occurred during the installation, exit code: {exitcode}");
                    });
            }
        } catch (Exception ex)
        {
            log.Warn(ex);
        }

在单击第一个按钮之前,您可以禁用第二个按钮

(或)

您也可以查看此 link 以获取任何想法 Wait for method to finish

您可以像这样添加静态 isRunning 类型字段:

void Install_1_Click(object sender , RoutedEventArgs e)
{
    RunSilentSetup("C:\app1.exe");
}

void Install_2_Click(object sender , RoutedEventArgs e)
{
    RunSilentSetup("C:\app2.exe");
}

static bool setupIsRunning = false;

void RunSilentSetup(string executableFilePath)
{
    if(setupIsRunning)
        return;

    setupIsRunning = true;
    //disable both buttons
    Install_1.Enabled = false;
    Install_2.Enabled = false;

    //code that runs the setup goes here

    setupIsRunning = false; //set back to false when finished

    //re-enable both buttons
    Install_1.Enabled = true;
    Install_2.Enabled = true;
}

有多种方法可以做到这一点,但这是我能想到的最简单的方法。

现在,当调用 RunSilentSetup 时,它首先检查 setupIsRunning 是否为真。如果是,它将return(无需进一步执行RunSilentSetup方法。

如果不是,它将 setupIsRunning 设置为 true - 以停止后续调用。

完成后,setupIsRunning 设置回 false。

简单,如果是WPF,创建一个bool IsWorking然后通过绑定转换器将其转换为启用的按钮属性,那么你的问题就解决了^^

也许你可以定义一个标志 属性 和启用,在忙时禁用按钮

 public bool IsBusyInstalling
    {
        get
        {
            return _isBusyInstalling;
        }
        private set
        {
            _isBusyInstalling = value;
            RaisePropertyChanged("IsBusyInstalling");
        }
    }

和简约风格:

 <Style TargetType="Button">
       <Style.Triggers>
              <DataTrigger Binding="{Binding Path=IsBusyInstalling}" Value="True">
                   <Setter Property="IsEnabled" Value="False"/>
               </DataTrigger>
        </Style.Triggers>
   </Style>