如何修复连接到外部进程的c#应用程序中的挂起问题

如何修复连接到外部进程的c#应用程序中的挂起问题,c#,visual-studio,networking,server,tor,C#,Visual Studio,Networking,Server,Tor,我正在用C#编写一个管理器应用程序,为网络服务器软件设计一个GUI,一个作为Web服务器的软件的GUI,其中包含一个用户必须输入配置的文本文件 在我正在设计的GUI中,用户输入他们的配置,然后单击按钮启动流程。它启动服务器软件并输入用户配置没有问题,但问题是web服务器软件启动后应用程序会冻结。我认为这是因为它在等待服务器软件响应时“挂起”,这是它自己的过程,因此不会直接与GUI应用程序交互。我想知道,有没有一种方法可以在启动GUI应用程序后将其从网络服务器可执行文件中分离出来,这样就不会出现这

我正在用C#编写一个管理器应用程序,为网络服务器软件设计一个GUI,一个作为Web服务器的软件的GUI,其中包含一个用户必须输入配置的文本文件

在我正在设计的GUI中,用户输入他们的配置,然后单击按钮启动流程。它启动服务器软件并输入用户配置没有问题,但问题是web服务器软件启动后应用程序会冻结。我认为这是因为它在等待服务器软件响应时“挂起”,这是它自己的过程,因此不会直接与GUI应用程序交互。我想知道,有没有一种方法可以在启动GUI应用程序后将其从网络服务器可执行文件中分离出来,这样就不会出现这种情况?非常感谢

代码如下:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        MessageBox.Show("I am now making sure Tor is running the newest version. If it is not, I will autmatically update tor to the newest verison. Please one minute. You may see CMD flash at you");
        PowerShell ps = PowerShell.Create();
        string script = string.Format("[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))");
        ps.AddScript(script);
        ps.Invoke();
        PowerShell ps1 = PowerShell.Create();
        string script1 = string.Format("choco install tor");
        ps1.AddScript(script1);
        ps1.Invoke();
        if (File.Exists(@"C:\\torrc.txt"))
        {
            MessageBox.Show("It looks like you already have a torrc file written. I will start tor now with these configurations. To change the torrc file, use the following window.");
            PowerShell psprestart = PowerShell.Create();
            string starttor = string.Format("tor.exe -f C:\\torrc.txt");
            psprestart.AddScript(starttor);
            psprestart.Invoke();
        }
    }







    public static string config;
    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {
        config = richTextBox1.Text;
        richTextBox1.EnableContextMenu();

    }

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Torrc file created. Now launching your relay. Please see the above window for the progress. If you see 'Testing indicates your ORPort is reachable. Publishing server descriptor, all is good. If you see a failure message, either your tor configuration is incorrect or the connection is being blocked. I will configure Windows Defender Firewall to allow the relay traffic autommatically, but any other firewall will need to be manually configured to allow Tor.");
        System.IO.File.WriteAllText(@"C:\torrc.txt", config);
        PowerShell ps2 = PowerShell.Create();
        string scriptfirewallrule = string.Format("netsh advfirewall firewall add rule name = 'Tor Relay' dir =in action = allow program = 'C:\\ProgramData\\chocolatey\\lib\\tor\\tools\\Tor\\tor.exe' Enable = yes");
        PowerShell psout = PowerShell.Create();
        string scriptfirewallruleout = string.Format("netsh advfirewall firewall add rule name = 'Tor Relay' dir =out action = allow program = 'C:\\ProgramData\\chocolatey\\lib\\tor\\tools\\Tor\\tor.exe' Enable = yes");
        psout.AddScript(scriptfirewallruleout);
        psout.Invoke();
        PowerShell ps3 = PowerShell.Create();
        string starttor = string.Format("tor.exe -f C:\\torrc.txt");
        ps3.AddScript(starttor);
        ps3.Invoke();
        MessageBox.Show("To see tor progress, open the notices.log file. It will be where you set it to be in the torrc configuration above.");



    }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

    }

    private void button2_Click(object sender, EventArgs e)
    {
        PowerShell ps3 = PowerShell.Create();
        string starttor = string.Format("tor.exe -f C:\\torrc.txt");
        ps3.AddScript(starttor);
        ps3.Invoke();
        MessageBox.Show("To see tor progress, open the notices.log file. It will be where you set it to be in the torrc configuration above.");

       
    }

您需要将单击按钮时运行的任务分解为按钮调用的异步方法。您可能想考虑如何用进度条或其他一些指示器来“返回”到GUI,任务完成。请看一个很好的例子:您需要将单击按钮时运行的任务分解为按钮调用的异步方法。您可能想考虑如何用进度条或其他一些指示器来“返回”到GUI,任务完成。下面是一个很好的例子: