C# backgroundworker.CancellationPending上的退出函数==true

C# backgroundworker.CancellationPending上的退出函数==true,c#,wpf,backgroundworker,C#,Wpf,Backgroundworker,我目前正试图让我的函数在backgroundworker.CancellationPending==true时取消自身 我有一门课是这样的: public class ImportPostManager { public event ProgressChangedEventHandler ProgressChanged; protected virtual void OnProgressChanged(int progress) { if (Progre

我目前正试图让我的函数在
backgroundworker.CancellationPending==true
时取消自身

我有一门课是这样的:

public class ImportPostManager
{
    public event ProgressChangedEventHandler ProgressChanged;

    protected virtual void OnProgressChanged(int progress)
    {
        if (ProgressChanged != null)
        {
            ProgressChanged(this, new ProgressChangedEventArgs(progress, null));
        }
    }

    public void DoSomeStuff()
    {
        while(something == true)
        {
            if(backgroundworker.CancellationPending) return; //Here's my problem

            //KeepDoingStuff and report progress
        }
    }
}
void _progressDialog_DoWork(object sender, DoWorkEventArgs e)
{
    Managers.ImportPostManager.Instance.ProgressChanged += (s, pe) => dialog.ReportProgress(pe.ProgressPercentage, null, pe.ProgressPercentage.ToString() + "%);
    Managers.ImportPostManager.Instance.DoSomeStuff();
}
我从
backgroundworker.DoWork()
中的表单调用函数,但是如何让类侦听
backgroundworker.CancellationPending
?我无法在
.DoWork()
中执行此操作,因为它看起来是这样的:

public class ImportPostManager
{
    public event ProgressChangedEventHandler ProgressChanged;

    protected virtual void OnProgressChanged(int progress)
    {
        if (ProgressChanged != null)
        {
            ProgressChanged(this, new ProgressChangedEventArgs(progress, null));
        }
    }

    public void DoSomeStuff()
    {
        while(something == true)
        {
            if(backgroundworker.CancellationPending) return; //Here's my problem

            //KeepDoingStuff and report progress
        }
    }
}
void _progressDialog_DoWork(object sender, DoWorkEventArgs e)
{
    Managers.ImportPostManager.Instance.ProgressChanged += (s, pe) => dialog.ReportProgress(pe.ProgressPercentage, null, pe.ProgressPercentage.ToString() + "%);
    Managers.ImportPostManager.Instance.DoSomeStuff();
}

…我不希望将backgroundworker发送到函数
.DoSomeStuff()
。有没有一种方法可以使用事件或其他东西来获得我想要的东西?我还没有找到任何东西可以解释如何以我能理解的方式…

如果您使用的是
DoSomeStuff
而不是直接传递给worker,并且假设它是阻塞的(这看起来很公平),那么您最好的选择可能是在函数中添加一个
Func
参数

public void DoSomeStuff(Func<bool> ShouldCancel)
{
    while (whatever == whateverElse && !ShouldCancel())
    {
        // Do work
    }
}
每次都会得到评估,生活会很美好

尽管您经常担心此类情况的发生,但还是值得研究更高级的多线程形式,即使只是通过
System.Threading.Tasks
名称空间提供。在那里,您将有更多的取消选项。但是,如果您正在寻找快速而简单的方法,添加该函数就可以了,并且它可以在多个上下文中工作,因此如果您将来选择不使用后台工作程序,那么您的helper类就不会被绑定到后台工作程序的使用上


当然,如果您不受
BackgroundWorker.CancellationPending
属性(或者如果我没记错的话,
Cancel()
方法)的约束,您可以更改该函数并签入
CancellationToken
,然后简单地针对该属性调用
Cancel()
(或者更确切地说,是针对其
CancellationTokenSource
)。这在某些方面并不理想,但如果你最终想要移动到更高级的某个位置(如我之前所说的高级)。当然,如果你现在设置了函数,你也可以很容易地在
DoSomeStuff
上分层令牌,所以你不会永远选择其中一个根据您现在的选择。

这似乎正是我想要的。我也会看看其他的选择,非常感谢Matthew。