Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.InvokeRequired)
{
txtbox1.Invoke(new MethodInvoker(delegate { txtbox1.Text = string.Empty; }));
}
like image 266
Sev Avatar asked Jun 28 '11 00:06

Sev


4 Answers

Try

if (toolStripProgressBar1.Parent.InvokeRequired)
{
    toolStripProgressBar1.Parent.Invoke(new MethodInvoker(delegate { toolStripProgressBar1.Value= 100; }));
}
like image 97
Bala R Avatar answered Oct 18 '22 20:10

Bala R


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.

like image 20
Enigmativity Avatar answered Oct 18 '22 20:10

Enigmativity


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.

like image 2
Danpe Avatar answered Oct 18 '22 20:10

Danpe


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);
    }
}
like image 1
flodis Avatar answered Oct 18 '22 18:10

flodis