2011-06-28 22 views
4

我正在使用下面的代碼來調用我的應用程序中主UI線程上的控件。我在狀態欄中的進度條沒有InvokeRequired,我需要以某種方式調用System.Windows.Forms.ToolStripProgressBar。如何在狀態欄中調用進度條?

if (txtbox1.InvokeRequired) 
{ 
txtbox1.Invoke(new MethodInvoker(delegate { txtbox1.Text = string.Empty; })); 
} 
+1

@Sev:如果您使用System.Windows.Forms.Controls.ProgressBar那麼它包含InvokeRequeired方法,因爲它是從控制和控制派生實現ISynchronizeInvoke接口。 –

+0

我正在使用System.Windows.Forms.ToolStripProgressBar – Sev

+0

我可以使用backgroundWorker只爲這個單一的控制?我不想重做整個線程代碼 – Sev

回答

7

嘗試在這個方便的擴展方法

if (toolStripProgressBar1.Parent.InvokeRequired) 
{ 
    toolStripProgressBar1.Parent.Invoke(new MethodInvoker(delegate { toolStripProgressBar1.Value= 100; })); 
} 
+4

謝謝,但沒有家長。取而代之,我做了 prgBarMain.GetCurrentParent()。InvokeRequired – Sev

+1

'Parent'列在'ToolStringProgressBar'的Properties下面http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripprogressbar.aspx。不知道爲什麼你沒有它。 –

+3

父屬性是內部的.net 4.5 –

2

嘗試調用ToolStrip的,而不是ToolStripProgressBar:

delegate void ToolStripPrograssDelegate(int value); 
    private void ToolStripPrograss(int value) 
    { 
     if (toolStrip1.InvokeRequired) 
     { 
      ToolStripPrograssDelegate del = new ToolStripPrograssDelegate(ToolStripPrograss); 
      toolStrip1.Invoke(del, new object[] { value }); 
     } 
     else 
     { 
      toolStripProgressBar1.Value = value; // Your thingy with the progress bar.. 
     } 
    } 

我不知道它會工作,但給它一拍。

如果這不會工作,試試這個:

delegate void ToolStripPrograssDelegate(int value); 
    private void ToolStripPrograss(int value) 
    { 
     if (this.InvokeRequired) 
     { 
      ToolStripPrograssDelegate del = new ToolStripPrograssDelegate(ToolStripPrograss); 
      this.Invoke(del, new object[] { value }); 
     } 
     else 
     { 
      toolStripProgressBar1.Value = value; // Your thingy with the progress bar.. 
     } 
    } 

「這個」應該是它的自我的形式。

+0

我正在用以下方法向進度條添加步驟。我如何在這裏使用你的代碼? public void AddProgressBarValue(int _step) { prgBarMain.Value + = _step; } – Sev

1

嘗試下探:

public static class ControlEx 
{ 
    public static void Invoke(this System.Windows.Forms.Control @this, Action action) 
    { 
     if (@this == null) throw new ArgumentNullException("@this"); 
     if (action == null) throw new ArgumentNullException("action"); 
     if (@this.InvokeRequired) 
     { 
      @this.Invoke(action); 
     } 
     else 
     { 
      action(); 
     } 
    } 
} 

現在,你可以這樣做:

txtbox1.Invoke(() => toolStripProgressBar1.Value = value); 

它安全地調用UI線程上的作用並可以從任何實際控制中調用。

+0

這是非常方便的擴展。非常感謝。 – Siva

+0

pitty你沒有回答這個問題,讓我們看看使用進度條的示例。 – scott

+1

@scott - 使用進程欄的例子就在我的答案中。 – Enigmativity

0

可以改變從Automating the InvokeRequired code pattern通用調用設計調用任何控制:在ToolStrip的

//Neat generic trick to invoke anything on a windows form control class 
//https://stackoverflow.com/questions/2367718/automating-the-invokerequired-code-pattern 
//Use like this: 
//object1.InvokeIfRequired(c => { c.Visible = true; }); 
//object1.InvokeIfRequired(c => { c.Text = "ABC"; }); 
//object1.InvokeIfRequired(c => 
// { 
//  c.Text = "ABC"; 
//  c.Visible = true; 
// } 
//); 
public static void InvokeIfRequired<T>(this T c, Action<T> action) where T : Control 
{ 
    if (c.InvokeRequired) 
    { 
     c.Invoke(new Action(() => action(c))); 
    } 
    else 
    { 
     action(c); 
    } 
} 

對象是的ToolStripItem並沒有InvokeRequired。但是,父母有與上述可改寫使用父.Invoke():

public static void InvokeIfRequiredToolstrip<T>(this T c, Action<T> action) where T : ToolStripItem 
{ 
    if (c.GetCurrentParent().InvokeRequired) 
    { 
     c.GetCurrentParent().Invoke(new Action(() => action(c))); 
    } 
    else 
    { 
     action(c); 
    } 
}