Domanda

I have (hopefully) a straight forward question. I have a function that runs a command prompt command in a hidden window and returns the response in a string. This process takes about 3 seconds. I wanted to add a simple label in my GUI that would appear before the function executes. The label just states that something is being checked so the user does not think the interface is just slow or unresponsive.

Here is an example snippet to illustrate.

        svnPathCheck_lbl.Visible = true; //Show the label     

        // Check validity of SVN Path
        string svnValidity = getCMDOutput("svn info " + SVNPath_txtbox.Text);

        // Here we call Regex.Match. If there is a 'Revision:' string, it was successful
        Match match = Regex.Match(svnValidity, @"Revision:\s+([0-9]+)", RegexOptions.IgnoreCase);

        svnPathCheck_lbl.Visible = false; //Hide the label

The getCMDOutput() function runs the hidden command and blocks the GUI.

What I expected this to do was display my label "Checking ...", then run the blocking function getCMDOutput(). Once the function returned and the GUI was responsive again, it would hide the label.

Instead, I never see the label show up at all. Its almost like it never executed. Could it be that the blocking function executes before the GUI has a chance to update?

Thanks for the help!

È stato utile?

Soluzione

try this code, it should work...

    private void button1_Click(object sender, EventArgs e)
    {
        svnPathCheck_lbl.Text = "Checking...";
        svnPathCheck_lbl.Visible = true;

        BackgroundWorker bw = new BackgroundWorker();
        bw.DoWork += bw_DoWork;
        bw.RunWorkerCompleted += bw_WorkCompleted;
        bw.RunWorkerAsync();
    }

    private void bw_WorkCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        svnPathCheck_lbl.Text = "Work completed";
    }

    private void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        string svnValidity = getCMDOutput("svn info " + SVNPath_txtbox.Text);
        Match match = Regex.Match(svnValidity, @"Revision:\s+([0-9]+)", RegexOptions.IgnoreCase);
    }

Altri suggerimenti

I recommend that you run your getCMDOutput method asynchronously.

If this is a windows forms application you can do this using a BackgroundWorker. Handle the event DoWork of the worker to call your method, and where you were calling it previously, put instead backgroundWorker1.RunWorkerAsync()

This will cause the method to run in a new thead, so the UI updates will be treated separately and will be instantaneous.

try this:

svnPathCheck_lbl.Visible = true; //Show the label     
Task connectToSVN = new Task(() => { this.connectToSVN; }); connectToSVN.Start(); //Open new Task to complite the code without blocking the GUI.

private void connectToSVN 
{
    // Check validity of SVN Path
    string svnValidity = getCMDOutput("svn info " + SVNPath_txtbox.Text);

    // Here we call Regex.Match. If there is a 'Revision:' string, it was successful
    Match match = Regex.Match(svnValidity, @"Revision:\s+([0-9]+)", RegexOptions.IgnoreCase);
    this.Dispatcher.Invoke((Action)(() =>
    {
       svnPathCheck_lbl.Visible = false; //Hide the label
    }
    ));
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top