简体   繁体   English

隐藏ProgressChanged事件

[英]Hide ProgressChanged event

Is it possible to hide the ProgressChanged event in a backgroundworker class? 是否可以在backgroundworker类中隐藏ProgressChanged事件?

I have created a class that inherits System.ComponentModel.BackgroundWorker and I need to have multiple events: My class processes a list of objects and I need (for example) to have events before and after an object is processed. 我创建了一个继承System.ComponentModel.BackgroundWorker的类,我需要有多个事件:我的类处理一个对象列表,我需要(例如)处理一个对象之前和之后的事件。 I fire those events from the Backgroundworker's ProgressChanged event, because it is thread safe. 我从Backgroundworker的ProgressChanged事件中触发这些事件,因为它是线程安全的。 The method in my class that is called when I invoke ReportProgress then uses the parameter to determine which event to fire. 我调用ReportProgress时调用的类中的方法然后使用该参数来确定要触发的事件。 That works. 这样可行。

Now I want to make sure, that the class calling my Backgroundworker is not allowed to subscribe to the ProgressChanged event directly. 现在,我要确保,该类打电话给我的BackgroundWorker 不允许订阅ProgressChanged直接的事件。 Instead it should only be possible to subscribe to the additional events that I provide. 相反,它应该只能订阅我提供的其他事件。

There are a few approaches: 有几种方法:

Disallow subscription and suppress firing 禁止订阅并禁止触发

The subclass can be written to (a) throw an exception if a caller attempts to subscribe directly to the ProcessChange event, and (b) not raise the event at all. 可以将子类写入(a)如果调用者尝试直接订阅ProcessChange事件,则抛出异常,(b)根本不引发事件。 Note that (a) alone does not prevent the event from being subscribed to, since callers can cast the subclass as a BackgroundWorker and subscribe to the event directly: 请注意,(a)单独不会阻止事件被订阅,因为调用者可以将子类强制转换为BackgroundWorker并直接订阅事件:

class ExtendedBackgroundWorker : BackgroundWorker
{
    public new event ProgressChangedEventHandler ProgressChanges
    {
        add { throw new InvalidOperationException("This event cannot be added directly"); }
        remove {}
    }

    protected override void OnProgressChanged(ProgressChangedEventArgs e)
    {
        // do not call base.OnProgressChanged
    }
}

Use composition in place of inheritance 使用组合代替继承

If possible, however, a better approach may be to not inherit from BackgroundWorker at all. 但是,如果可能,更好的方法可能是根本不从BackgroundWorker继承。 A BackgroundWorker uses the ProgressChanged event to report progress changes; BackgroundWorker使用ProgressChanged事件来报告进度更改; if it does not do so, it is not a BackgroundWorker . 如果它不这样做,它不是BackgroundWorker Instead, consider implementing the BackgroundWorker as a private class member and exposing members from the member as needed, eg: 相反,请考虑将BackgroundWorker实现为私有类成员,并根据需要向成员公开成员,例如:

class CustomBackgroundWorker : Component
{
    private BackgroundWorker worker;
    public event ProgressChangedEventHandler FirstEvent;
    public event ProgressChangedEventHandler SecondEvent;
    public event DoWorkEventHandler DoWork
    {
        add { worker.DoWork += value; }
        remove { worker.DoWork -= value; }
    }
    public event RunWorkerCompletedEventHandler RunWorkerCompleted
    {
        add { worker.RunWorkerCompleted += value; }
        remove { worker.RunWorkerCompleted -= value; }
    }

    public CustomBackgroundWorker()
    {
        worker = new BackgroundWorker();
        worker.ProgressChanged += OnProgressChanged;
        worker.WorkerReportsProgress = true;
    }

    public void RunWorkerAsync()
    {
        worker.RunWorkerAsync();
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
        worker.Dispose();
    }

    private void OnProgressChanged(object o, ProgressChangedEventArgs e)
    {
        // code to handle progress change reports from the worker
    }
}

Refire event to be of use to client 为客户提供有用的活动

Rather than suppressing use of the event, the subclass can use it in conjunction with (rather than as a substitute for) the other events to provide relevant information to the subscriber (eg, the progress of the total operation). 子类可以将其与其他事件结合使用(而不是作为其替代)来向订户提供相关信息(例如,总操作的进度),而不是抑制事件的使用。

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

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