Parallel.Foreach
을 실행하는 동안 진행률 막대를 업데이트하려고하지만 실행 중에 아무 것도 발생하지 않습니다. Progressbar는 For 루프가 끝날 때만 업데이트됩니다. 이 코드를 어떻게 작동시킬 수 있습니까?WPF : Parallel.Foreach에서 진행률 막대 업데이트
XAML이 답변에서
<StackPanel>
<Grid x:Name="LoadProgressGrid" Height="100"
Visibility="Visible">
<ProgressBar x:Name="LoadProgress"
Maximum="100"
Minimum="1" />
<TextBlock Margin="0,0,0,-5"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="16"><Run Text="Import Progress"/></TextBlock>
</Grid>
<Button Height="35" Width="100" Margin="0,10" Content="Test" Click="Test_Click"/>
</StackPanel>
C#
private void Test_Click(object sender, RoutedEventArgs e)
{
decimal current=0;
List<string> lst = new List<string>();
lst.Add("Foo");
lst.Add("Foo");
lst.Add("Foo");
lst.Add("Foo");
decimal max = 100000;
var uiFactory = new TaskFactory(TaskScheduler.FromCurrentSynchronizationContext());
Parallel.ForEach(lst, (data) =>
{
for (int i = 0; i < max; i++)
{
// Use the uiFactory above:
// Note the need for a temporary here to avoid closure issues!
current = current + 1;
uiFactory.StartNew(() => LoadProgress.Value = (double)(current/max)*100);
}
});
MessageBox.Show("Done!");
}
LoadProgress.Value는 정수가 아니고 정수가 아닌가요? – Vyrira
UI에서 실제로 작업을 수행 할 수있게하려면 UI 스레드를 차단하지 마십시오. – Servy
클로저를 사용하지 않기 위해 임시로 만들지는 않으므로 임시로 만드는 것이 아닙니다. 당신은 단지 클로저를 계속 사용합니다. – Servy