简体   繁体   English

调用委托(方法)

[英]Invoke Delegate (Method)

I got some code that uses a delegate to pass the percentage of the operation. 我得到了一些使用委托来传递操作百分比的代码。 But the delegate method is run on the same thread as the operation, which is a backgroundworker. 但是,委托方法是在与该操作相同的线程上运行的,它是一个后台工作器。 So updating a progressbar in the delegate method is impossible without invocation. 因此,如果没有调用,就不可能在委托方法中更新进度条。

Should I just invoke in the delegate method or is there a better way? 我应该只调用委托方法还是有更好的方法? I didn't really understand the example that is on msdn (it was in vb.net too which made it harder :/). 我真的不明白msdn上的示例(它也在vb.net中,这使得它更加困难:/)。

You could add something along the lines of the below to the form containing the progress bar. 您可以将以下内容添加到包含进度条的表单中。 This will check to see if an invoke is required. 这将检查是否需要调用。

public void SetProgressValue(int value)
{
    if (this.ProgressBar.InvokeRequired)
    {
       this.BeginInvoke(new Action<int>(SetProgressValue), value);
       return;
    }
    this.ProgressBar.Value= value;
}

Dispatcher.BeginInvoke((Action)()=> YourDelegate());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM