简体   繁体   中英

UI Doesn't Show ProgressBar Progress in WPF Project

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM