2016-06-29 4 views
-2

私はC#WPFデスクトップアプリケーションを持っています。「仕事」が「働いている」間にプログレスバーを表示する方法

私はいくつかの仕事をしたいと思います。私がしたいときは、完了するのを待っている間に進行状況バー/情報を表示したいと思います。私はこのNO「こんにちはアンディ」を実行しない場合

public MainWindow() 
{ 
    InitializeComponent(); 
} 

private void ShowBusy() 
{ 
    Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, 
     new Action(() => 
     { 
      frm.Visibility = Visibility.Collapsed; 
      prog.Visibility = Visibility.Visible; 
     })); 
} 

private void HideBusy() 
{ 


    Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, 
     new Action(() => 
     { 
      frm.Visibility = System.Windows.Visibility.Visible; 
      prog.Visibility = System.Windows.Visibility.Collapsed; 
     })); 
} 

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    System.Threading.Thread th = new System.Threading.Thread(ShowBusy); 
    th.Start(); 

    while (true) 
    { 
     System.Threading.Thread.Sleep(100); 
    } 

    HideBusy(); 
} 

表示され、無限ループだけ実行されます。

<Window xmlns:mui="http://firstfloorsoftware.com/ModernUI" x:Class="WpfApplication3.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:WpfApplication3" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid HorizontalAlignment="Center" VerticalAlignment="Center"> 
     <StackPanel x:Name="frm" Orientation="Horizontal" > 
      <Button Click="Button_Click" Content="Press Me!" /> 
      <Label Content="this is my test stuff"></Label> 
     </StackPanel> 
     <TextBox Text="Hello Andy" x:Name="prog" Visibility="Collapsed"></TextBox> 
    </Grid> 
</Window> 

マイコードの後ろ:

ので、これをモックアップするために私がこのUIを持っています..

+0

コメントは議論の対象外です。この会話は[チャットに移動]されています(http://chat.stackoverflow.com/rooms/115974/discussion-on-question-by-andrew-simpson-how-to-display-a-progress-bar-whilst- w)。 –

答えて

2
private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    Thread th = new Thread(DoSomething); 
    th.Start(); 
} 

private void DoSomething() 
{ 
    ShowBusy(); 
    while (true) // No Exit Clause ??? 
    { 
     System.Threading.Thread.Sleep(100); 
    } 
    HideBusy(); 
} 
+0

こんにちは、スリープはすべてのスレッドをブロックしていませんでした。私は無限ループ待ちをカウンタで置き換えました。それで感謝しました:) –

+0

@AndrewSimpson Sleepはすべてのスレッドをブロックしません。呼び出したスレッドをブロックします。 –

+0

私は3分で答えを受け入れることができます –

1

非同期タスクは、あなたの長いRの進捗状況を知ることができIProgress<T>を持っています保全方法。

private async void Button_Click(object sender, RoutedEventArgs e) 
{ 
    IProgress<int> progress = new Progress<int>(); 
    progress.ProgressChanged += progress_ProgressChanged; 

    await DoSomething(progress); 
} 

private void progress_ProgressChanged(object sender, int e) 
{ 
    if(e < 100) 
    { 
     ShowBusy(e); //Show busy with progress percentage 
    } 
    else 
    { 
     HideBusy(); 
    } 
} 

private async Task DoSomething(IProgress<int> progress) 
{ 
    progress.Report(0) 
    longMethod1(); 
    progress.Report(50) 
    longMethod2(); 
    progress.Report(100) 
} 
+0

これは便利です - ありがとう –

関連する問題