【问题标题】:Update ProgressBar in another window from a function?从函数更新另一个窗口中的进度条?
【发布时间】:2011-09-04 23:30:52
【问题描述】:

我有一个上传文件的后台任务,我希望它向另一个表单上的进度条报告进度。我该怎么办?我对 C# 有点陌生,但长期从事 VB .net 程序员。

我弄乱了这段代码,但它完全错误。

System.Windows.Forms.Form progForm = new ProgressWindows();
Control[] ctrls = progForm.Controls.Find("fileProgress1", true);

【问题讨论】:

  • Winform 应用程序?你想显示确切的百分比吗?你有什么办法知道百分比吗?你想使用 BacgroundWorker 吗?很抱歉有很多问题:-)
  • 我确实知道。它是一个 MS VSTO Outlook 插件。但是在传输过程中,我使用 percentComplete = (int)(totalReadCount / (double)fileSize * 100);我得到了一个简单的 1 到 100 的进步。我已经通过消息框对此进行了测试。

标签: c# .net winforms progress-bar


【解决方案1】:

如果您使用的是BackgroundWorker,则只需致电ReportProgress。否则,您需要将 UI 更改分派到正确的线程。在 WinForms 中查看Control.InvokeRequired 属性和相关方法。 WPF 等效项是DispatcherObject.VerifyAccess。

编辑:Visual Studio 不在我面前,所以可能会有一些小的编译错误。

public partial class MyForm : Form
{
    public MyForm()
    {
        InitializeComponent(); // fileProgress1 setup. 
    }

    private void StartTask()
    {
        Task t1 = new Task(BackgroundMethod1, fileProgress1); // Explicitly pass a reference to the progress bar.
        Task t2 = new Task(BackgroundMethod2); // Use a method that has access to the bar.
        Task t5 = new Task(BackgroundMethod3, IncrementPBMethod); // Pass an action to the background method. Abstracting the physical progress bar as something where you can set the progress.
        Task t4 = new Task(delegate() { /* fileProgress1 referened*/ }); // Create a closure. I don't recommend this method.
    }

    private static void BackgroundMethod1(ProgressBar pb)
    {
        for(int i = 0; i < 100; ++i)
        {
            if(pb.InvokeRequired)
            {
                pb.Invoke(delegate() { pb.Value = i; });
            }

            Thread.Sleep(1000);
        }
    }

    private void BackgroundMethod2()
    {
        for(int i = 0; i < 100; ++i)
        {
            if(fileProgress1.InvokeRequired)
            {
                fileProgress1.Invoke(delegate() { fileProgress1.Value = i; });
            }

            Thread.Sleep(1000);
        }
    }

    private static BackgroundMethod3(Action<int> setProgress)
    {
        for(int i = 0; i < 100; ++i)
        {
            setProgress(i);
            Thread.Sleep(1000);
        }
    }

    private void IncrementPBMethod(int value)
    {
        if(fileProgress1.InvokeRequired)
        {
            fileProgress1.Invoke(IncrementPBMethod, value);
        }
        else
        {
            fileProgress1.Value = value;
        }
    }
}

【讨论】:

  • 我正在使用 System.Threading.Tasks。无论哪种方式,我只想从任务内部设置值。问题是,我无法访问表单内部的控件。在 VB 中,这类似于 Dim progbar1 as ProgressBar = CType(ProgressWindows.FindControl("fileProgress1), ProgressBar)。
  • 而不是尝试使用FindControl将进度条控件传递给Task的构造函数或传递具有访问权限的方法。
  • 给我一点时间写点东西。
【解决方案2】:

如果您的问题只是访问 fileProgress1。最简单的解决方案是在 ProgressWindow 类中公开它。默认情况下,控件是私有的。

然后你可以访问fileProgress1控件如下。

ProgressWindows progForm = new ProgressWindows();
//progForm.fileProgress1 

但更好的方法是在 PrefressWindows 类中公开一个公共方法来更新进度。

在 ProgressWindows 类中

public void UpdateProgressBar(int percentage)
{
    // Set the progress in the progress bar.
    fileProgress1.Percentage = percentage;
}

如下调用上述方法。

ProgressWindows progForm = new ProgressWindows();
progForm.UpdateProgressBar(percentage);

【讨论】:

  • @Dan C:你做了什么?第一种方式还是第二种方式?两者都应该工作。但第二种是首选方式。
  • 我明白为什么按照您所说的那样调用该方法会更好,但是我在任务中得到了这个:错误 1 ​​'System.Windows.Forms.Form' 不包含'找不到 UpdateProgressBar 并且没有扩展方法“UpdateProgressBar”接受“System.Windows.Forms.Form”类型的第一个参数(您是否缺少 using 指令或程序集引用?)
  • 第二种方式,看最后一篇:)
  • ProgressWindows 类在不同的库中?更新代码后你编译了吗?
  • 不,它在同一个库中,当我开始输入 ProgressW 时,Intellisense 就会启动......是的,我尝试编译,不行。
【解决方案3】:

我假设您的第二个表单(您想要显示进度的那个)在执行“任务”的表单中被实例化。为什么不简单地在第二种形式中创建一个公共方法,除了一个整数参数来更新进度条值?似乎是一个相对简单的解决方案。

【讨论】:

  • 那是因为表单不会从执行任务的表单中实例化。这是一个 Outlook 插件。在报告进度之前会有一个 if 语句查看窗口是否打开。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多