取消本地c++ /CLI包装中的BackgroundWorker。

时间:2022-01-17 04:33:09

Like this post I need to cancel a BackgroundWorker in a C++/CLI class derived from a .NET BackgroundWorker.

就像这篇文章,我需要取消一个从。net BackgroundWorker派生的c++ /CLI类的BackgroundWorker。

I am using C++/CLI to wrap a long running, legacy, native C++ function.

我使用c++ /CLI来包装长期运行的、遗留的、本机c++函数。

How can/should I access the CancellationPending member of the BackgoundWorker class from the long running loop in the native C++?

在本地c++的长运行循环中,如何访问被取消的工人类的成员?

public ref class BackgroundWorkerWrapper : BackgroundWorker
{
    private:
        Outline* pOutline;

    public:
        BackgroundWorkerWrapper(void) 
        {
            WorkerReportsProgress = true;

            pOutline = new Outline;

    //      pOutline->CancellationPending = &(BackgroundWorker::CancellationPending);

        }

    protected:

        virtual void OnDoWork(DoWorkEventArgs ^e) override {
            pOutline->DoWork();
            BackgroundWorker::OnDoWork(e);
        }

    };

Above you can see I have tried to map the CancellationPending to a bool in the Outline class. So far this has not worked.

在上面你可以看到,我试着把取消的日期映射到大纲类的bool。到目前为止,这还没有奏效。

My Outline class is fairly straight forward.

我的提纲是相当直接的。

public class Outline
{

public:
    bool* CancellationPending;

    Outline()
    {
    }
    ~Outline()
    {
    }

    void DoWork()
    {
        try
        {
            for (int i=0; i < 1000; i++)
            {
                if (CancellationPending[0] == true)
                {
                    break;
                }

                // Do some trivial work
                Sleep(50);
            }
        }
        // catch(...)
              catch (exception& e)
        {
            cout << e.what() << endl;
        }

    }
};

Alternately is there another way to add another handler to the CancelAsnc() function of the BackgroundWorker class that could somehow set a cancel variable in the outline class?

另一种方法是将另一个处理程序添加到BackgroundWorker类的cancbounnc()函数中,该函数可以在outline类中设置一个cancel变量?

Thanks.

谢谢。

1 个解决方案

#1


0  

You could add something to the end of your OnDoWork override to continuously check the BackgroundWorker::CancellationPending flag. If true, set pOutline->CancellationPending to true too.

您可以在OnDoWork覆盖的末尾添加一些内容,以连续检查BackgroundWorker::取消挂起的标志。如果是正确的,设置pOutline->取消等待为真。

#1


0  

You could add something to the end of your OnDoWork override to continuously check the BackgroundWorker::CancellationPending flag. If true, set pOutline->CancellationPending to true too.

您可以在OnDoWork覆盖的末尾添加一些内容,以连续检查BackgroundWorker::取消挂起的标志。如果是正确的,设置pOutline->取消等待为真。