2015-03-30 15 views
1

我們應用程序的主窗口打開了一個應該調用後臺線程的進行中工作對話框。應該會出現對話框,直到線程完成:現在如何在正在進行的工作對話框中正確調用後臺線程?

var dialog = new WorkInProgressDialog(); 
dialog = ShowDialg; 

問題出在哪裏/如何調用線程WorkInProgressDialog構造?如果它在構造函數中被調用,那麼在線程完成之前對話框將不可見。

此外,線程完成後應該自動關閉對話框。

+0

許多選項 - 你可以使用'Thread' ,'BackgroundWorker'或理想的新任務庫,例如'Task.Factory.StartNew' – vesan 2015-03-30 22:24:57

+0

可能的重複[在程序運行時顯示信息消息](http://stackoverflow.com/ques tions/27052090/display-an-information-message-while-the-program-is-running) – 2015-03-30 23:39:03

+1

有很多關於StackOverflow的類似問題,包括建議的重複。你應該提供[一個很好的,_minimal_,_complete_代碼示例](http://stackoverflow.com/help/mcve),它顯示了你到目前爲止所嘗試的內容,以及對代碼的作用和不同之處的精確解釋從你想要/希望它做的事情。 – 2015-03-30 23:40:14

回答

1

這是一個可以幫助你的樣本。對於WorkInProgressDialog一些簡單的標記:

<Window x:Class="WorkInProgressExample.WorkInProgressDialog" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="WorkInProgressDialog" Height="100" Width="300" WindowStyle="None" 
    ResizeMode="NoResize" WindowStartupLocation="CenterScreen"> 
    <Border BorderThickness="3" CornerRadius="5" BorderBrush="Black" Padding="10"> 
     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="*"></RowDefinition> 
       <RowDefinition Height="Auto"></RowDefinition> 
       <RowDefinition Height="Auto"></RowDefinition> 
       <RowDefinition Height="*"></RowDefinition> 
      </Grid.RowDefinitions>    

      <TextBlock Grid.Row="1" Name="WorkProgressTextBlock">Work in progress...</TextBlock> 
      <ProgressBar Grid.Row="2" Height="30" Name="WorkProgressBar"></ProgressBar> 
     </Grid> 
    </Border> 
</Window> 

然後在後面的代碼:

private bool _closeAuthorised = false; 

public WorkInProgressDialog() 
{ 
    InitializeComponent(); 

    WorkProgressBar.Maximum = 10; 
    WorkProgressBar.Minimum = 0; 

    Task.Factory.StartNew(() => 
    { 
     // Do whatever processing you need to here 
     for (int i = 0; i < 10; i++) 
     { 
      // Any updates to the UI need to be done on the UI thread 
      this.Dispatcher.Invoke((Action)(() => 
      { 
       WorkProgressBar.Value = i; 
      })); 

      Thread.Sleep(1000); 
     } 

     // Set the DialogResult and hence close, also on the UI thread 
     this.Dispatcher.Invoke((Action)(() => 
     { 
      _closeAuthorised = true; 
      this.DialogResult = true; 
     })); 

    }); 
} 

protected override void OnClosing(System.ComponentModel.CancelEventArgs e) 
{ 
    // If the user uses ALT+F4 to try andclose the loading dialog, this will cancel it 
    if (!_closeAuthorised) 
     e.Cancel = true; 
    base.OnClosing(e); 
} 

然後,你要使用它:

var dialog = new WorkInProgressDialog(); 
bool? result = dialog.ShowDialog(); 
+0

非常感謝您的幫助示例。只有將DialogResult設置爲true才能關閉WorkInProgressDialog? – 2015-03-31 10:04:50

+0

沒問題。不,如果將this.DialogResult設置爲false,對話框也會關閉。所不同的是,從你顯示對話框的位置(在我的例子中的最後一個代碼塊)bool?結果將是錯誤的,而不是真實的。例如,你可以用它來表示一個錯誤條件。 – 2015-03-31 10:24:11

+0

如果我誤解了你的問題,我還調整了包含處理的答案,如果用戶試圖用ALT + F4關閉對話框 – 2015-03-31 10:43:26

相關問題