locked
WPF Application window showing Not Responding RRS feed

  • Question

  • hi ,

    I have developed an WPF application which uses long running sql procedures

    so i used background worker so that the application wouldn't hang but

    it still display "Not Responding" for some time or in some cases longer

    please give some pointers regarding it.

    Friday, July 19, 2013 5:03 AM

Answers

  • Hi,

    Make it very simple, you dont have to use reportprogress.

    Check below sample.

    Codebehind

     BackgroundWorker bgw = new BackgroundWorker();  
            public Window3()
            {
                InitializeComponent();
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
                bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
                bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
                bgw.WorkerReportsProgress = true;
                bgw.RunWorkerAsync();
            }
            void bgw_DoWork(object sender, DoWorkEventArgs e)
            {
                int total = 57; //some number (this is your variable to change)!!
    
                for (int i = 0; i <= total; i++) //some number (total)
                {
                    System.Threading.Thread.Sleep(100);
                    int percents = (i * 100) / total;
                    bgw.ReportProgress(percents, i);
                }
            }
    
            void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                txt.Text = String.Format("Progress: {0} %", e.ProgressPercentage);
            }
    
            void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                //do the code when bgv completes its work
            }

    XAML

     <StackPanel HorizontalAlignment="Left"  Background="#BFC0C2"   >
            <Button Content="Click it" Height="30" Width="100" Margin="10" Click="Button_Click"/>
            <StackPanel Orientation="Horizontal" Margin="10">
                <TextBlock Text="Percentage :" />
                <TextBlock x:Name="txt" />
            </StackPanel>
        </StackPanel>

    Thanks,

    Rajnikant


    • Marked as answer by Lisa Zhu Saturday, July 27, 2013 7:05 AM
    Friday, July 19, 2013 6:40 AM

All replies

  • Hi,

    Can you add some more information or code, means what exactly you are doing with backgroundworker.

    Thanks,

    Rajnikant

    Friday, July 19, 2013 5:39 AM
  • hi

    i have an datagrid which contains 1000 or more rows of items

    on button click i get selected items and pass it to background process.

    MessageBoxResult result = MessageBox.Show("Once Archive is started,all active user connection to PLM will be disconnected which may result in data loss.\nAre you sure you want to start Archive process?", "Confirm Archive Process", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.Yes);
     if (result == MessageBoxResult.Yes)
      {
            }
    else if (result == MessageBoxResult.No)
                    {
                        return;
                    }
    progressBar1.Visibility = Visibility.Visible;
                    txtStatus.Text = "";
                    Status = "";
    
    Dictionary<string, string> dicArg = new Dictionary<string, string>();
    dicArg.Add("SeasonYearID", cboSeasons.SelectedValue.ToString());
                    dicArg.Add("SeasonYear", cboSeasons.Text.ToString());
                    objProductManager._ModuleFlag = 1;
                    objProductManager.DeleteDataFromBulk();
                    DataTable dt = objProductManager.GetColumnNames();               
                    objProductManager._NoOfArchive = objProductManager.GetMaxNoOfArchive() + 1;
                    objProductManager.InsertIntoHistory();
    
                    StringBuilder Items = new StringBuilder();
    
                    int count = 0;
                    DataRow dr;
                    for (int j = 0; j < dgStyles.Items.Count; j++)
                    {
                        DataGridCell cell = GetCell(j, 0);
                        CheckBox ck = GetVisualChild<CheckBox>(cell);
                        if (ck.IsChecked == true)
                        {
                            dr = dt.NewRow();
                            DataRowView s = (DataRowView)dgStyles.Items[j];
                            count++;
    
                            if (Items.Length == 0)
                            {
                                Items.Append(s.Row["product_fabric_id"].ToString());
                            }
                            else
                            {
                                Items.Append("," + s.Row["product_fabric_id"].ToString());
                            }
                            dr["product_fabric_id"] = s.Row["product_fabric_id"].ToString();
                            dr["module_id"] = 1;
                            dr["node_id"] = s.Row["node_id"].ToString();
                            dr["no_of_archive"] = objProductManager._NoOfArchive;
                            dt.Rows.Add(dr);
                        }
                    }
                    if (count == 0)
                    {
                        MessageBox.Show("Please Select Items..");
                        progressBar1.Visibility = Visibility.Hidden;
                        return;
                    }
                    btnArchive.IsEnabled = false;
                    btnBack.IsEnabled = false;
                    btnNext.IsEnabled = false;
                    btnSkip.IsEnabled = false;
                    btnView.IsEnabled = false;
                    dgStyles.IsEnabled = false;
                    int bulkresult = objProductManager.InsertBulkData(dt);
                    if (bulkresult == 1)
                    {
                        dicArg.Add("Products", Items.ToString());
                        this.backgroundWorker.RunWorkerAsync(dicArg);
                    }

    in background process i start calling procedure by looping through items

    before that i call backgroundWorker.ReportProgress

    like this 

     DoEvents();
                    Thread.Sleep(1000);
                    Status = "Processing.....";
                    backgroundWorker.ReportProgress(1);

    after for some extra status update

    Friday, July 19, 2013 5:59 AM
  • Hi,

    Make it very simple, you dont have to use reportprogress.

    Check below sample.

    Codebehind

     BackgroundWorker bgw = new BackgroundWorker();  
            public Window3()
            {
                InitializeComponent();
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
                bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
                bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
                bgw.WorkerReportsProgress = true;
                bgw.RunWorkerAsync();
            }
            void bgw_DoWork(object sender, DoWorkEventArgs e)
            {
                int total = 57; //some number (this is your variable to change)!!
    
                for (int i = 0; i <= total; i++) //some number (total)
                {
                    System.Threading.Thread.Sleep(100);
                    int percents = (i * 100) / total;
                    bgw.ReportProgress(percents, i);
                }
            }
    
            void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                txt.Text = String.Format("Progress: {0} %", e.ProgressPercentage);
            }
    
            void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                //do the code when bgv completes its work
            }

    XAML

     <StackPanel HorizontalAlignment="Left"  Background="#BFC0C2"   >
            <Button Content="Click it" Height="30" Width="100" Margin="10" Click="Button_Click"/>
            <StackPanel Orientation="Horizontal" Margin="10">
                <TextBlock Text="Percentage :" />
                <TextBlock x:Name="txt" />
            </StackPanel>
        </StackPanel>

    Thanks,

    Rajnikant


    • Marked as answer by Lisa Zhu Saturday, July 27, 2013 7:05 AM
    Friday, July 19, 2013 6:40 AM
  • i have to let user know what is happening so i have to use report progress

    is there any other alternative?

    Friday, July 19, 2013 6:50 AM
  • Hi,

    Check progresschanged event, in that you can let user know, how much percentage completed.

    Run sample code given in my previous thread.

    Thanks,

    Rajnikant

    Friday, July 19, 2013 7:00 AM