0

I have a problem in my WPF app. I have a custom CircularProgressBar. When I retrieve data from database it takes a few seconds.

I would like to show the CircularProgressBar in my app while the data is retrieved.

This code runs the CircularProgressBar :

CircularProgressBar cb = new CircularProgressBar();
stk.Children.Add(cb);

ThreadStart thStart = delegate()
{
    ThreadStart inv = delegate()
    {
        stk.Children.Remove(cb);

    };
    this.Dispatcher.Invoke(inv, null);
};

Thread myThread = new Thread(thStart);
myThread.Start();

in my custom class (Printer).

And where I call this window:

Printer p = new Printer();

p.Show();

//Code For retrieve  Data from DataBase

p.close();

So this happens : CircularProgressBar shows for a few seconds and it not running. Where is my bug?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    Use a background worker. [This might help](http://stackoverflow.com/questions/5483565/how-to-use-wpf-background-worker) – musefan Mar 14 '13 at 17:51
  • @abbas Pirmoradi : use background worker and update progress bar value with progreschanged event. – KF2 Mar 14 '13 at 18:06

4 Answers4

1

You can simply use background worker:

private 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);

private void bw_DoWork(object sender, DoWorkEventArgs e)
{
//load data from database
System.Threading.Thread.Sleep(1);
worker.ReportProgress(progressbar_value);
}

private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
Progress.value= progressbar_value;
}

private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//progress completed
}
KF2
  • 9,887
  • 8
  • 44
  • 77
0

This is not how you do stuff in wpf - use a model to populate the data from the db and than bind CircularProgressBar visibility to the state you're in (hide it when you completed the task). all this boilerplate code should be in xaml.

Maor Hayoun
  • 17
  • 1
  • 3
0

If I were you, I would simplify life by using databinding with dependencyproperties. What are the steps to follow.

1) Create a dependency property called IsBusyProperty of type bool in your custom progressbar.

2) Register a delegate to its value change event (this is done when you create the dependency property).

3) You can now bind this IsBusyProperty to a status in your code that says hey I am busy.

4) When the value is set to true you get your progressbar to start its magic.

5) When it is set to false you stop the magic.

TYY
  • 2,702
  • 1
  • 13
  • 14
0

It is far simpler to create a control with a storyboard that rotates, so long as your ui is not locked it will rotate then simply kill it afterward.

Try this

Telimaktar
  • 31
  • 2