Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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>
C#
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
Posted
Updated 13-Dec-18 7:10am

1 solution

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.
 
Share this answer
 
Comments
TheBigBearNow 13-Dec-18 21:11pm    
How do I invoke report progress from dowork()?

Application.Current.Dispatcher.Invoke(delegate
17
    {
18
        backgroundWorker.ReportProgress(); //but what do i pass in and not sure if this is it
19
    });

Richard Deeming 14-Dec-18 10:23am    
You don't need to wrap that call in Invoke - ReportProgress will automatically take care of that for you.

The value you pass in will a number representing the progress, and an optionally an object of your own choice. You can then access these values in the ProgressChanged event handler.

NB: e.UserState could be null, so your current code will throw a NullReferenceException when you call e.UserState.ToString(). It's easy enough to fix:
LblWatch.Content = "Processing " + e.UserState + " : " + e.ProgressPercentage;
// Or:
LblWatch.Content = string.Format("Processing {0} : {1}", e.UserState, e.ProgressPercentage);
// Or:
LblWatch.Content = $"Processing {e.UserState} : {e.ProgressPercentage}";

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900