2013-04-02 34 views
0

像這樣post我需要取消從.NET BackgroundWorker派生的C++/CLI類中的BackgroundWorker。取消本機C++循環的C++/CLI包裝中的BackgroundWorker

我正在使用C++/CLI來包裝長時間運行的傳統本機C++函數。

我該如何/應該從本地C++的長時間運行循環訪問BackgoundWorker類的CancellationPending成員?

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); 
     } 

    }; 

上面你可以看到我試圖將CancellationPending映射到Outline類的bool中。到目前爲止,這還沒有奏效。

我的大綱課程相當簡單。

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; 
     } 

    } 
}; 

或者有另一種方式到另一個處理程序添加到的BackgroundWorker類的CancelAsnc()函數,它能夠以某種方式在大綱類中設置取消變量?

謝謝。

回答

0

您可以在OnDoWork覆蓋的末尾添加一些內容以連續檢查BackgroundWorker::CancellationPending標誌。如果爲true,則也將pOutline->CancellationPending設置爲true。