开发者

How to Invoke the progress bar in Status strip?

I am using the following code to invoke the controls that are on the main UI thread in my application. The progress bar that I have in my Status Strip does not have a InvokeRequired, I need to somehow invoke the System.Windows.Forms.ToolStripProgressBar.

if (txtbox1.开发者_如何学CInvokeRequired)
{
txtbox1.Invoke(new MethodInvoker(delegate { txtbox1.Text = string.Empty; }));
}


Try

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


Try dropping in this handy extension method:

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();
        }
    }
}

Now you can just do this:

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

It safely calls the action on the UI thread and can be called from any actual control.


Try invoking the ToolStrip and not the 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..
        }
    }

I'm not sure it will work, but give it a shoot.

If this wont work try this:

    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..
        }
    }

'this' should be the Form it's self.


You can alter the generic invoker from Automating the InvokeRequired code pattern designed to invoke any Control:

//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);
    }
}

Objects in the ToolStrip are ToolStripItem and have no InvokeRequired. But the parent has and the above can be rewritten to use the parent .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);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜