Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL .NET WPF C#4.0
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
            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:
  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 6-Apr-13 2:46am
Edited 6-Apr-13 11:31am
v4
Comments
ThePhantomUpvoter at 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 at 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."
ThePhantomUpvoter at 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 at 6-Apr-13 15:58pm
   
thanks,but I need to close it after finish loading data
ThePhantomUpvoter at 6-Apr-13 17:57pm
   
Well then you only have one option.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi
 
try to replace
Dispatcher.BeginInvoke(new Action(() =>
with
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.
  Permalink  
Comments
abbaspirmoradi at 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 at 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 at 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)

  Print Answers RSS
0 BillWoodruff 330
1 Mathew Soji 309
2 Maciej Los 295
3 DamithSL 225
4 Afzaal Ahmad Zeeshan 217
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,025


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 6 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100