我已經問過一個類似問題here但我現在有一個後續問題。讓BackgroundWorker按順序執行多個操作,而不凍結表格
我需要多次運行外部程序成一排,但我有幾個問題是:
- 它試圖同時啓動所有操作。我已經把一個空的「while(bgwkSVN.IsBusy){}」,它有點作用,但我很確定它會讓你們中的一些人哭一點。
- 只要所有的操作都沒有完成,它仍會凍結表單。考慮到其他幾個SO主題,我想我的代碼編寫方式,應用程序並不是真正的多線程,或者我沒有利用它......但我對線程真的不熟悉。
- 它似乎沒有做我所要求的。我會嘗試更簡單的操作,看看操作是否成功,或者如果背景工作者從未啓動。
下面是代碼(抱歉,這是有點長):
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;
}
謝謝!
我做到了,但現在我可以無法訪問DoWork方法中的表單控件?而且,由於我只啓動一個BackgroundWorker,所以在進程運行時我無法更新輸出? – thomasb 2009-12-15 10:25:23
您無法在兩者之前訪問控件。將任何DoWork需求放在svnCommand類中。它可以更新輸出,使用ReportProgress – 2009-12-15 13:13:59
你是對的,我在沒有調試的情況下啓動程序(用web dev開發的習慣),結果發現後臺工作人員在後臺默默地崩潰。所以這就是爲什麼沒有真正做到。 – thomasb 2009-12-15 14:49:59