【问题标题】:How to Invoke the progress bar in Status strip?如何调用状态条中的进度条?
【发布时间】:2017-11-26 22:07:53
【问题描述】:

我正在使用以下代码来调用应用程序中主 UI 线程上的控件。我的状态条中的进度条没有 InvokeRequired,我需要以某种方式调用 System.Windows.Forms.ToolStripProgressBar。

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

【问题讨论】:

  • @Sev:如果你使用 System.Windows.Forms.Controls.ProgressBar 那么它包含 InvokeRequeired 方法,因为它是从 Control 派生的,并且 Control 实现了 ISynchronizeInvoke 接口。
  • 我正在使用 System.Windows.Forms.ToolStripProgressBar
  • 我可以只为这个单一控件使用 backgroundWorker 吗?我不想重做整个线程代码
  • @Sev: 然后使用this.InvokeRequired,因为ToolStripProgressBar 和表单中创建的所有其他控件都是在同一个表单线程中创建和初始化的。
  • System.Windows.Forms.ToolStripProgressBarprgBarMain.GetCurrentParent().InvokeRequired 是万一有人需要的解决方案

标签: c# .net multithreading


【解决方案1】:

试试

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

【讨论】:

  • 谢谢,但没有家长。相反,我做了这个 prgBarMain.GetCurrentParent().InvokeRequired
  • Parent 列在 ToolStringProgressBar msdn.microsoft.com/en-us/library/… 的属性下。不知道为什么你没有它。
  • 父属性是 .net 4.5 的内部属性
【解决方案2】:

试试这个方便的扩展方法:

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 线程上的操作,并且可以从任何实际控件中调用。

【讨论】:

  • 这是一个非常方便的扩展。非常感谢。
  • 可惜你没有回答问题,让我们根据要求看一个使用进度条的例子。
  • @scott - 使用进程栏的示例就在我的答案中。
【解决方案3】:

尝试调用 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..
        }
    }

'this' 应该是它自己的表单。

【讨论】:

  • 我正在通过以下方法将步骤添加到进度条。我怎样才能在这里使用你的代码?公共无效 AddProgressBarValue(int _step) { prgBarMain.Value += _step; }
【解决方案4】:

您可以从 Automating the InvokeRequired code pattern 更改通用调用程序,用于调用任何控件

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

ToolStrip 中的对象是 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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 2012-09-03
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 2021-01-27
    • 1970-01-01
    相关资源
    最近更新 更多