做一个BackgroundWorker做几次作业顺序不结冰的形式 [英] Make a BackgroundWorker do several operations sequentially without freezing the form

查看:162
本文介绍了做一个BackgroundWorker做几次作业顺序不结冰的形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经问过somwhat类似的问题<一href="http://stackoverflow.com/questions/1880500/refresh-form-controls-with-external-program-output">here但我现在有一个后续的问题。

I have already asked a somwhat similar question here but I now have a follow-up question.

我需要多次运行外部程序成一排,但我有几个问题是:

I need to launch the external program several times in a row, but I have several problems with it :

  • 在它试图同步推出的所有操作。我已经把一个空的,而(bgwkSVN.IsBusy){},它有点工作,但我是pretty的肯定,这将使得一些你哭了一点点。
  • 它仍冻结的形式,只要所有的操作没有完成。考虑到一些其他SO话题,我觉得我的code被写入的方式,应用程序是不是真的多线程的,或者我不好好利用这个机会......但我真的不熟悉线程。
  • 这似乎并不做什么,我要求它做的事。我将用更简单的操作试试,看看是否操作并不成功,或者如果BackgroundWorker的是永远不会启动。

下面是code(对不起,它有点长):

Here is the code (sorry, it's kinda long) :

private struct svnCommand
{
    public svnCommand(string args, string path, int pourcent)
    {
        this.args = args;
        this.path = path;
        this.pourcent = pourcent;
    }
    public string args;
    public string path;
    public int pourcent;
}

private BackgroundWorker bgwkSVN;

public Merger()
{
    InitializeComponent();
    InitializeBackgroundWorker();
    this.textBoxCheminRacine.Text = cheminRacine;
}

private void MergerRevisions(object sender, EventArgs e)
{

    activerControles(false);

    textBoxOutput.Text = "";
    cheminRacine = textBoxCheminRacine.Text;
    if (!cheminRacine.EndsWith("\\")) { cheminRacine = cheminRacine + "\\"; }

    string branchToMerge = this.textBoxBranche.Text;
    if (branchToMerge.StartsWith("/")) { branchToMerge = branchToMerge.Substring(1); }

    // révision(s)
    string revisions = "";
    foreach (string r in textBoxRevision.Text.Split(','))
    {
        int rev;
        if (int.TryParse(r, out rev))
        {
            revisions += string.Format(" -r {0}:{1}", rev - 1, rev);
        }
        else
        {
            revisions += " -r " + r.Replace("-", ":");
        }
    }

    // pourcentage de complétion pour chaque étape
    int stepPourcent = (int)Math.Floor((double)(100 / (3 + Directory.GetDirectories(cheminRacine + "branches").Length)));

    // merge sur le trunk
    while (bgwkSVN.IsBusy) { }
    bgwkSVN.RunWorkerAsync(new svnCommand(string.Format("merge --accept postpone {0} {1}{2} .", revisions, svnbasepath, branchToMerge), cheminRacine + "trunk", stepPourcent));


    // merge sur chaque branche
    string[] branches = Directory.GetDirectories(cheminRacine + "branches");
    foreach (string b in branches)
    {
        while (bgwkSVN.IsBusy) { }
        bgwkSVN.RunWorkerAsync(new svnCommand(string.Format("merge --accept postpone {0} {1}{2} .", revisions, svnbasepath, branchToMerge), b, stepPourcent));
    }

    // virer les mergeinfo
    while (bgwkSVN.IsBusy) { }
    bgwkSVN.RunWorkerAsync(new svnCommand("pd svn:mergeinfo . -R", cheminRacine, stepPourcent));

    // svn update
    while (bgwkSVN.IsBusy) { }
    bgwkSVN.RunWorkerAsync(new svnCommand("update", cheminRacine, stepPourcent));

    textBoxOutput.Text += Environment.NewLine + "Terminé.";
    MessageBox.Show("Merge terminé.", "Merge terminé", MessageBoxButtons.OK);

    // réactiver les champs et boutons
    activerControles(true);
}

/// <summary>
/// Set up the BackgroundWorker object by attaching event handlers
/// </summary>
private void InitializeBackgroundWorker()
{
    bgwkSVN = new BackgroundWorker();
    bgwkSVN.WorkerReportsProgress = true;
    bgwkSVN.WorkerSupportsCancellation = true;
    bgwkSVN.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
    bgwkSVN.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
    bgwkSVN.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
}

/// <summary>
/// Exécuter une commande SVN
/// </summary>
private string SVNcmd(svnCommand s, BackgroundWorker worker, DoWorkEventArgs e)
{
    string o = "";
    o += s.path + Environment.NewLine + s.args + Environment.NewLine;

    if (worker.CancellationPending)
    {
        e.Cancel = true;
    }
    else
    {
        Process p = new Process();
        p.StartInfo.WorkingDirectory = s.path;
        p.StartInfo.FileName = "svn";
        p.StartInfo.Arguments = s.args;
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.UseShellExecute = false;
        p.Start();
        o += p.StandardOutput.ReadToEnd() + Environment.NewLine;
        p.WaitForExit();

        if (s.pourcent > 0)
        {
            worker.ReportProgress(s.pourcent);
        }
    }
    return o;
}


/// <summary>
/// Where the actual, potentially time-consuming work is done.
/// </summary>
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    // Get the BackgroundWorker that raised this event.
    BackgroundWorker worker = sender as BackgroundWorker;

    // Assign the result of the computation to the Result property of the DoWorkEventArgs
    // object. This is will be available to the RunWorkerCompleted eventhandler.
    e.Result = SVNcmd((svnCommand)e.Argument, worker, e);
}

/// <summary>
/// Deals with the results of the background operation
/// </summary>
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // First, handle the case where an exception was thrown.
    if (e.Error != null)
    {
        MessageBox.Show(e.Error.Message);
    }
    else if (e.Cancelled)
    {
        textBoxOutput.Text += Environment.NewLine + "Annulé.";
    }
    else
    {
        textBoxOutput.Text += e.Result.ToString();
    }
}

/// <summary>
/// Updates the progress bar
/// </summary>
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    this.progressBarTraitement.Value += e.ProgressPercentage;
}

谢谢!

推荐答案

解决的办法很简单:有一个BGW执行的所有的命令的,不只是一个BGW每个命令。你需要一个名单,其中,svnCommand&GT; 存储命令,这样就可以轻松地将它们传递给的RunWorkerAsync()。 DoWork的()可以简单地迭代列表的foreach。

The solution is simple: have one BGW execute all of the commands, not just one BGW for each command. You'll need a List<svnCommand> to store the commands so you can easily pass them to RunWorkerAsync(). DoWork() can simply iterate the list with foreach.

这篇关于做一个BackgroundWorker做几次作业顺序不结冰的形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆