簡體   English   中英

調用對話框並在backgroundworker中從中獲取值?

[英]calling dialog and getting values from it in backgroundworker?

我正在一個WPF項目中,我必須從許多單個文件中導入數據。 這些文件及其中的數據的實際導入是通過backgroundworker doWork方法完成的。 它就像一個咒語,可以完成工作,並且更新進度條也可以完美地工作。

現在,根據我在那些文件中遇到的情況,有時我需要從用戶處獲得一個決定,然后才能繼續處理當前文件。

什么是打開窗口/對話框,將其中設置的值返回到backgroundworker.doWork方法並繼續處理的最佳方法?

后台工作人員甚至可能做到這一點,還是我需要將該處理邏輯保留在main / UI線程中,並以某種方式從那里更新進度條?

我希望你們中的一些人能給我一些提示或指向其他資源,因為我沒有找到有關我的特定問題的有用信息。

您可以在new ThreadBackgroundWorker上調用OpenFileDailog classShowDialog (這也會創建一個新的線程)

但是,當您想要傳遞或更新在主線程上運行的任何屬性或控件時,您將需要使用Disptacher如下所示

Dispatcher.BeginInvoke(new Action(() => { YourMethodThatUpdatesSomething(); }));

后台工作者在另一個線程中工作。 您不能直接從后台線程調用UI。 實現您要執行的操作的一種方法是使用標志和分派器調用UI進行用戶輸入

    bool WaitFor_Input = false; //flag to indicate user input status

    private void ThreadRun()
    {
        //THIS IS IN Background worker thread
        //do task
        WaitFor_Input = true;
        //ask for user input
        Dispatcher.BeginInvoke(new Action(Show_Dialogue), null);
        while (WaitFor_Input); //Background worker waits untill user input is complete
        //continue further processing
    }

    private void Show_Dialogue()
    {
        //THIS IS IN UI THREAD
        //show your dialogue box here, update data variables
        //finally set
        WaitFor_Input = false;
    }

將處理邏輯保留在后台線程中實際上是一個好主意。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM