在WPF c#中使用带有LINQtoSQL的.mdf数据库的程序,当我加载数据库时,会有一点冻结

时间:2022-09-24 18:00:30

OK I have the following code that I run inside a button clicked event in order to compare the username and password given by the user.

我有下面的代码,我运行在一个按钮点击事件,以比较用户给出的用户名和密码。

public static bool isAuthenticated(string Username, string Password)
    {
        //Open a connection with the database
        using (WHDataDataContext db = new WHDataDataContext())
        {
            //Compare the Username and the password and return the result
            return db.Users.Any(check => check.Username == Username && check.Password    == Cryptographer.Encrypt(Password));
        }
    }

My problem is that when I hit the button the program freezes for a moment and the it responds.

我的问题是,当我点击按钮时程序冻结了一会儿,然后它就回复了。

I have used this code on a c# application with .sdf file (SQL CE) and I haven't experienced this issue.

我在一个c#应用程序中使用了这段代码,其中包含.sdf文件(SQL CE),我没有遇到过这个问题。

Could anyone please help?

有人能帮忙吗?

3 个解决方案

#1


0  

It's because you are doing a blocking activity (like database query) in the UI Thread. Try to use BackgroundWorker

这是因为您正在UI线程中执行阻塞活动(如数据库查询)。尝试使用BackgroundWorker

using (var worker = new BackgroundWorker())
{
    worker.DoWork += (theSender, theArgs) =>
    {
        theArgs.Result = isAuthenticated(userName, password);
    };
    worker.RunWorkerCompleted += (theSender, theArgs) =>
    {
        bool isValid = (bool)theArgs.Result;
        if(!isValid)
        {
            MessageBox.Show("Not Authenticated!!");
        }
    };
    worker.RunWorkerAsync();
}

#2


0  

Try using async task instead.

尝试使用异步任务。

public static async Task<bool> isAuthenticated(string Username, string Password)
{
    return await Task.Factory.StartNew<bool>(()=>{
    //Open a connection with the database
    using (WHDataDataContext db = new WHDataDataContext())
    {
        //Compare the Username and the password and return the result
        return db.Users.Any(check => check.Username == Username && check.Password    == Cryptographer.Encrypt(Password));
    }
    }
}

#3


0  

Why not create the DB context once when the app starts? If it is the datacontext initialization that incurs the delay, move it to where your application starts instead.

为什么不在应用程序启动时创建一次DB上下文呢?如果导致延迟的是datacontext初始化,那么将它移动到应用程序开始的位置。

Since it is a WPF app it is presumably long running so no need to create a new DB context for each database retrieval.

因为它是一个WPF应用程序,所以运行时间很长,所以不需要为每个数据库检索创建新的DB上下文。

#1


0  

It's because you are doing a blocking activity (like database query) in the UI Thread. Try to use BackgroundWorker

这是因为您正在UI线程中执行阻塞活动(如数据库查询)。尝试使用BackgroundWorker

using (var worker = new BackgroundWorker())
{
    worker.DoWork += (theSender, theArgs) =>
    {
        theArgs.Result = isAuthenticated(userName, password);
    };
    worker.RunWorkerCompleted += (theSender, theArgs) =>
    {
        bool isValid = (bool)theArgs.Result;
        if(!isValid)
        {
            MessageBox.Show("Not Authenticated!!");
        }
    };
    worker.RunWorkerAsync();
}

#2


0  

Try using async task instead.

尝试使用异步任务。

public static async Task<bool> isAuthenticated(string Username, string Password)
{
    return await Task.Factory.StartNew<bool>(()=>{
    //Open a connection with the database
    using (WHDataDataContext db = new WHDataDataContext())
    {
        //Compare the Username and the password and return the result
        return db.Users.Any(check => check.Username == Username && check.Password    == Cryptographer.Encrypt(Password));
    }
    }
}

#3


0  

Why not create the DB context once when the app starts? If it is the datacontext initialization that incurs the delay, move it to where your application starts instead.

为什么不在应用程序启动时创建一次DB上下文呢?如果导致延迟的是datacontext初始化,那么将它移动到应用程序开始的位置。

Since it is a WPF app it is presumably long running so no need to create a new DB context for each database retrieval.

因为它是一个WPF应用程序,所以运行时间很长,所以不需要为每个数据库检索创建新的DB上下文。