C#WPF多线程后台工作者加载..gif图像作为登录 [英] C# WPF multithreading backgroundworker loading ..gif image as login

查看:155
本文介绍了C#WPF多线程后台工作者加载..gif图像作为登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,当我加载/检查我的数据库连接时,我没有运气加载gif图像我单击我的登录按钮我的登录中没有错误我启动了我的gif但我也开始在dowork和进度部分我不确定哪个部分实际上是正确的我知道不要在粪便中弄脏UI

我的程序运行但我的.gif不运行,一段时间后出现正确的屏幕!所以我知道我非常接近只是我的gif需要弹出,这就是全部。

我的gif在< mediaelement>

  private   void  BtnLoginUser_Click( object  sender) ,RoutedEventArgs e)
{ // 开始加载Gif
LoadSpinner.Visibility = Visibility.Visible;

// 禁用登录并取消创建按钮。
BtnLoginUser.IsEnabled = false ;
BtnCreateUser.Content = 取消;

// 启动后台工作程序。
backgroundWorker.RunWorkerAsync ();
}
私有 void BackgroundWorker_DoWork( object sender,DoWorkEventArgs e)
{
// ??开始加载??
// 抛出新的NotImplementedException();
// backgroundWorker.ReportProgress((int)e.Argument);
< span class =code-keyword> try
{
loginUser = SQLuserAccess.UserLogin(username,password);
if (loginUser!= null
{
if (username == loginUser.Username&& password == loginUser.Password)
{

}
}
}
catch (Exception ex){MessageBox.Show(ex.Message.ToString()); }
}
私有 void BackgroundWorker_ProgressChanged( object sender,ProgressChangedEventArgs e)
{
LblWatch.Content = 处理 + e.UserState.ToString()+ + e.ProgressPercentage ;

// 也许这里是我运行loading.gif的地方? ??可以在progressChanged中自由地与UI进行交互。

LoadSpinner.Visibility = Visibility.Visible;
}
私有 void BackgroundWorker_RunWorkerCompleted( object sender,RunWorkerCompletedEventArgs e)
{
// 停止加载RUNS当线程是COMPLETED可以在UI上工作
// 抛出新的NotImplementedException();
BtnCreateUser.Content = 注册;
LoadSpinner.Visibility = Visibility.Hidden;

if (loginUser.IsAdmin)
{
Window WindowAdminMenu = AdminWindow(loginUser);
WindowAdminMenu.Show();
关闭();
}
else if (loginUser.IsCustomer)
{
Window WindowCustomerMenu = new CustomerScreen(loginUser);
WindowCustomerMenu.Show();
关闭();
}
else
lblInvalidText.Content = 无效的帐户信息;
}
私有 void LoadSpinner_MediaEnded( object sender,RoutedEventArgs e)
{
LoadSpinner.Position = new TimeSpan( 0 0 1 );
LoadSpinner.Play();
}





我的尝试:



我运行没有错误唯一的原因是因为某些原因我的.gif没有弹出所以我的加载图像不起作用,而登录部分正在加载

解决方案
是;你在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.


这篇关于C#WPF多线程后台工作者加载..gif图像作为登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆