backgroundworker + wpf - >冻结的窗口

时间:2010-04-21 09:26:39

标签: c# wpf progress-bar backgroundworker

-progressbar总是0%

- 窗口是froozen(而DoWork r。)

-if System.threading.thread.sleep(1)on - 完美运行

问题是什么?

private void btnNext_Click(object sender, RoutedEventArgs e)
{
  this._worker = new BackgroundWorker();
  this._worker.DoWork += delegate(object s, DoWorkEventArgs args)
            {
                long current = 1;
                long max = generalMaxSzam();


                for (int i = 1; i <= 30; i++)
                {
                    for (int j = i+1; j <= 30; j++)
                    {
                        for (int c = j+1; c <= 30; c++)
                        {
                            for (int h = c+1; h <= 30; h++)
                            {
                                for (int d = h+1; d <= 30; d++)
                                {
                                    int percent = Convert.ToInt32(((decimal)current / (decimal)max) * 100);
                                    this._worker.ReportProgress(percent);
                                    current++;
                                    //System.Threading.Thread.Sleep(1); - it works well
                                }
                            }
                        }
                    }
                }
            };

            this._worker.WorkerReportsProgress = true;

 this._worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
            {
                this.Close();
            };

 this._worker.ProgressChanged += delegate(object s, ProgressChangedEventArgs args)
            {                              
                this.statusPG.Value = args.ProgressPercentage;             
            };

 this._worker.RunWorkerAsync();
}

<Window x:Class="SzerencsejatekProgram.Create"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Létrehozás" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="500" Width="700">    
    <DockPanel>    
        <Button DockPanel.Dock="Right"  Name="btnNext" Width="80" Click="btnNext_Click">Tovább</Button>
        <StatusBar DockPanel.Dock="Bottom">
            <StatusBar.ItemsPanel>
                <ItemsPanelTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="auto"/>
                            <ColumnDefinition Width="auto"/>
                        </Grid.ColumnDefinitions>
                    </Grid>
                </ItemsPanelTemplate>
            </StatusBar.ItemsPanel>
            <StatusBarItem Grid.Column="1">
                <TextBlock Name="statusText"></TextBlock>
            </StatusBarItem>
            <StatusBarItem Grid.Column="2">
                <ProgressBar Name="statusPG" Width="80" Height="18" IsEnabled="False" />
            </StatusBarItem>
            <StatusBarItem Grid.Column="3">
                <Button Name="statusB" IsCancel="True" IsEnabled="False">Cancel</Button>
            </StatusBarItem>
        </StatusBar>
    </DockPanel>
</Window>

3 个答案:

答案 0 :(得分:2)

您的代码运行一个非常紧凑的循环,并在其中心调用ReportProgress()。

这意味着您的MessageQueue会被执行进度更新的请求所淹没。

如果你在Bgw线程中构建一些延迟(Thread.Sleep(100)),你会看到响应能力的提高。

更实际的解决方案是将报告移至外部循环。在你的情况下:

for (int i = 1; i <= 30; i++)
{
    int percent = (i * 100) / 30;
    _worker.ReportProgress(percent);
    for(int j = 0; ....)
        ....
}

如果您只有1个循环,则构建延迟:'if((counter%100)== 0)...`

此处的目标是用户,目标是对Reportprogress进行10到100次调用。

答案 1 :(得分:0)

 if (current++ % onePercent == 0)
                    {
                         int percent = Convert.ToInt32(((decimal)current / (decimal)max) * 100);
                         this._worker.ReportProgress(percent, new WorkerUserState { current = current, max = max });                        
                    }

这很有效。

答案 2 :(得分:0)

ProgressChanged事件的匿名方法将在UI线程上运行。由于您报告了频繁的进度,因此它将由调度程序排队并阻止UI。