且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#WPF多线程后台工作者加载..gif图像作为登录

更新时间:2023-12-05 16:46:04

是;你在ProgressChanged处理程序中显示(新).gif文件(因为它在调用者的UI线程上运行而不是DoWork,后者是后台工作者线程)。



你必须从DoWork调用ReportProgress(调用ProgressChanged);它本身不会运行。


So far I haven’t had luck with my loading gif image when I am loading/checking my database connection I click my login button and I get no errors in my login I start my gif but I also start it in the dowork and progress section im not sure which section is actually right I know not to mess wih the UI in dowork
My program runs but my .gif does not run and after a while the correct screen appears! So I know I am super close just my gif needs to pop up and that is all.
My gif is in a <mediaelement>

private void BtnLoginUser_Click(object sender, RoutedEventArgs e)
        {       // Start loading Gif
                LoadSpinner.Visibility = Visibility.Visible;

                // Disable login and make create button a cancel.
                BtnLoginUser.IsEnabled = false;
                BtnCreateUser.Content = "Cancel";

                // Start the background worker.
                backgroundWorker.RunWorkerAsync();
 }
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            // ?? Start the loading ??
            //throw new NotImplementedException();
           // backgroundWorker.ReportProgress((int)e.Argument);
            try
            {
                loginUser = SQLuserAccess.UserLogin(username, password);
                if (loginUser != null)
                {
                    if (username == loginUser.Username && password == loginUser.Password)
                    {
                        
                    }
                }
            }
            catch(Exception ex) { MessageBox.Show(ex.Message.ToString()); }
  }
        private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            LblWatch.Content = "Processing " + e.UserState.ToString() + " : " + e.ProgressPercentage;

//maybe here is where i run the loading.gif?? ??    free to interact with UI in progressChanged.

            LoadSpinner.Visibility = Visibility.Visible;
}
private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //Stop the loading  RUNS when thread is COMPLETED can work on UI
            //throw new NotImplementedException();
            BtnCreateUser.Content = "Register";
            LoadSpinner.Visibility = Visibility.Hidden;

            if (loginUser.IsAdmin)
            {
                Window WindowAdminMenu = new AdminWindow(loginUser);
                WindowAdminMenu.Show();
                Close();
            }
            else if (loginUser.IsCustomer)
            {
                Window WindowCustomerMenu = new CustomerScreen(loginUser);
                WindowCustomerMenu.Show();
                Close();
            }
            else
                lblInvalidText.Content = "Invalid Account Information";
}
        private void LoadSpinner_MediaEnded(object sender, RoutedEventArgs e)
        {
            LoadSpinner.Position = new TimeSpan(0, 0, 1);
            LoadSpinner.Play();
}



What I have tried:

I got this running with no errors the only thing is for some reason my .gif isnt popping up so my loading image doesnt work while the login section is loading

Yes; you display the (new) .gif in the "ProgressChanged" handler (because it runs on the caller's UI thread versus DoWork which "is" the background worker thread).

And you have to call "ReportProgress" (to invoke ProgressChanged) from DoWork; it doesn't run by itself.