0

i have a Planner Application project , i have created a waiting User Control For The Times When i get the data from Database to show the progress to the user , so i write this codes for this purpose :

    public void LoadDestinationList()
    {
        Dispatcher.Invoke(new Action(() =>
        {
            WS_PPB.Value = 10;
            RDFDB_Class MyClass = new RDFDB_Class(TaskType);
            WS_PPB.Value = 25;
            Thread MyThread = new Thread(new ThreadStart(MyClass.LoadDataFromDb));
            WS_PPB.Value = 40;
            MyThread.IsBackground = true;
            MyThread.Start();
            WS_PPB.Value = 55;
            while (MyThread.IsAlive)
            {
                WS_PPB.Value = 75;
            }
            WS_PPB.Value = 100;
        }));
    }

but i don't know why The UserControl Loaded after the data retrieved from the data base and it only show 100% in the progressbar , could you tell me what is wrong here ? please help me to fix this problem.

1 Answers1

0

i found the solution here : Making a progress bar update in real time in wpf thank you greenjaed. my new codes :

        await Task.Run(() => 
        {

            WSMainUC.WS_PPB.Dispatcher.Invoke(() => WS_PPB.Value = 15, DispatcherPriority.Background);
            RDFDB_Class MyClass = new RDFDB_Class(TaskType);
            WSMainUC.WS_PPB.Dispatcher.Invoke(() => WS_PPB.Value = 30, DispatcherPriority.Background);
            Thread MyThread = new Thread(new ThreadStart(MyClass.LoadDataFromDb));
            MyThread.IsBackground = true;
            MyThread.Start();
            WSMainUC.WS_PPB.Dispatcher.Invoke(() => WS_PPB.Value = 50, DispatcherPriority.Background);
            while (MyThread.IsAlive)
            {
                WSMainUC.WS_PPB.Dispatcher.Invoke(() => WS_PPB.Value = 75, DispatcherPriority.Background);
            }
            WSMainUC.WS_PPB.Dispatcher.Invoke(() => WS_PPB.Value = 100, DispatcherPriority.Background);
        });
  • mixing tasks and threads is never a good idea - pick one or the other. – Peregrine Mar 26 '21 at 19:11
  • The task is for the ui but the thread doing something else in another project , in fact the thread load the data in data layer so i need a thread to do it in background and i need task to avoid ui freeze and update the ui , right now it's worked but i will find a better solution in future , thank you for you suggestion. – IKINGSHADOW Mar 26 '21 at 19:39