Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Every Body.
I Have CircularProgressBar that is a User Control.I Create a Window with Name Of Printer And Add CircularProgressBar to it.
In my App I will When I connect to Sql And Load Data CircularProgressBar is shown And when this Loading finished it disappear.
then I use BackgroundWorker thread for this Mean.this is my code:
in public
C#
Printer p = new Printer();
BackgroundWorker bw = new BackgroundWorker();
bw.WorkerReportsProgress = true;
bw.WorkerSupportsCancellation = true;
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);

When Button View Clicked:
C#
private void View_Click(object sender, RoutedEventArgs e)
      {
           bw.RunWorkerAsync();

      }
  private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
      {
          p.Close();
      }

      private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
      {
      }

      private void bw_DoWork(object sender, DoWorkEventArgs e)
      {
          Dispatcher.BeginInvoke(new Action(() =>
          {
              p.Show();
              ViewTabelsData();
          }));
      }

But It Do not work Correctly.

When Button View first clicked CircularProgressBar just for few seconds shown (that more less than it must shown)and then disappear.when button view for second time pressed I get this Exception: System.InvalidOperationException "Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed."


Where is my Bug?What the best way I do it?
Posted
Updated 6-Apr-13 10:31am
v4
Comments
[no name] 6-Apr-13 8:08am    
"But It Do not work Correctly" means nothing to us. What does not work correctly? What does it do? What is it supposed to do?
abbaspirmoradi 6-Apr-13 12:30pm    
Ok.When Button View first clicked CircularProgressBar just for few seconds shown (that more less than it must shown)and then disappear.when button view for second time pressed I get this Exception: System.InvalidOperationException "Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed."
[no name] 6-Apr-13 14:58pm    
Of course. You are closing the form in the bw_RunWorkerCompleted event and then when you click the button again you are trying to show the form. It's closed, over, done with. You either need to new it again or do not close it.
abbaspirmoradi 6-Apr-13 15:58pm    
thanks,but I need to close it after finish loading data
[no name] 6-Apr-13 17:57pm    
Well then you only have one option.

1 solution

Hi

try to replace
C#
Dispatcher.BeginInvoke(new Action(() =>

with
C#
Dispatcher.Invoke(new Action(() =>


It should help you with instant form closing.

And to fix InvalidOperationException you should create new instance of Printer every time when you want to show it, or use Hide method instead of close.

But in general it's not realy a good idea to show your from in bw_DoWork method, better place is right before bw.RunWorkerAsync(); method call.
 
Share this answer
 
Comments
abbaspirmoradi 7-Apr-13 8:46am    
thanks Volodymyr.
InvalidOperationException with your help is fixed.
I use this:
private void View_Click(object sender, RoutedEventArgs e)
{
p.Show();
bw.RunWorkerAsync();
}

private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
p.Hide();
}
but now CircularProgressBar just for few seconds shown (that more less than it must shown)and then disappear.And when I deleted p.Hide(); I saw when CircularProgressBar is shown, not rotated .and when loading data finished it rotation begining.
Volodymyr Bobko 7-Apr-13 11:16am    
As far I understand your ViewTabelsData function should not interact with UI element so you can rewrite function bw_DoWork

private void bw_DoWork(object sender, DoWorkEventArgs e)
{
ViewTabelsData();
}
abbaspirmoradi 8-Apr-13 5:57am    
thanks Volodymyr.my problem solved.I updated my UI in w_RunWorkerCompleted and problem was solved.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900