如何在后台最佳执行查询,以不冻结的应用程序(.NET) [英] How best execute query in background to not freeze application (.NET)

查看:91
本文介绍了如何在后台最佳执行查询,以不冻结的应用程序(.NET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WinForm的应用程序需要与显著的执行时间执行复杂的查询,我没有任何影响(约10分钟) 当执行查询时用户将看到应用程序没有响应任务管理器中,这实在是令人困惑的用户,也不够专业......

My WinForm apps needs to execute complex query with significant execution time, I have no influence (about 10mins) When query is executing user sees 'application not responding' in task manager, which is really confusing to user, also not very professional...

我相信,查询将在不同的线程左右执行。试过一些方法,但有困难,使其真正起作用(执行查询,迫使主要的应用等待结果,返回到主应用程序,可能要取消执行等)

I believe that query shall be executed in different thread or so. Have tried some approaches but have difficulties to make it really working (execute query, force main application wait result, return back to main app, possibility to cancel execution etc)

我不知道你有这自己的/良好的工作方案。 code的样品会也非常欢迎:)

I wonder if you have own / good working solution for that. Code samples would be also very welcome :)

此外,我相信有可能存在一些准备使用一些工具/框架允许简单执行的。

Also I believe there might exist some ready to use utilities / frameworks allowing simple execution of that.

推荐答案

如果的executeQuery ,如果需要的方法来执行,你可以这样做:

If ExecuteQuery if the method you want to execute, you can do:

void SomeMethod() {
    var thread = new Thread(ExecuteQuery);
    thread.Start();
}

void ExecuteQuery() {
    //Build your query here and execute it.
}

如果的executeQuery 接收一些参数,如:

If ExecuteQuery receives some parameters, like:

void ExecuteQuery(string query) {
    //...
}

您可以做的:

var threadStarter = () => { ExecuteQuery("SELECT * FROM [Table]"); };
var thread = new Thread(ThreadStarter);
thread.Start();

如果你想停止后台线程的执行,避免调用 Thread.Abort的()方法

。这将的线程,你不想这样,因为一些incosistency可能出现在你的数据库。

If you want to stop the execution of the background thread, avoid calling thread.Abort() method. That will kill the thread, and you do not want this, because some incosistency could appear in your database.

相反,你可以有一个布尔变量可见的executeQuery 键,从外面就可以将其设置为<$当你想停止C $ C>真。然后,所有你需要做的就是检查中的code里面的的executeQuery 一些地区,如果该变量仍然是。否则,做一些的回滚的维护数据库的稳定。

Instead, you can have a bool variable visible from ExecuteQuery and from outside you can set it to True when you want to stop it. Then all you have to do is check in some parts of the code inside ExecuteQuery if that variable is still True. Otherwise, do some rollback to maintain the database stable.

请确保您设置了布尔变量的 挥发性

Be sure you set that bool variable volatile

编辑:
如果你想在UI的的从后台线程,我通常做的:


If you want the UI to wait from the background thread, I usually do:

  • 启动后台线程
  • 在启动一些进度条在UI
  • 禁用的某些控件(如按钮等),以避免用户点击他们,而后台线程工作(例如:如果你执行线程,当用户点击一个按钮,然后你应该禁用按钮,否则多个查询将ocurring在同一时间)。
  • Start the background thread
  • Start some progress bar in the UI
  • Disable some controls (like buttons, etc) to avoid the user to click them while the background thread is working (eg: If you're executing the thread when a user clicks a button, then you should disable that button, otherwise multiple queries will be ocurring at the same time).

完成线程后,你可以停止进度条,并再次启用控件。

After finished the thread, you can stop progress bar and enable controls again.

如何知道当线程完成了吗?您可以使用该事件。只需创建一个事件,触发它,当它完成,并做你想做的事件处理中...

How to know when the thread finished? You can use Events for that. Just create an event and fire it when it finishes, and do whatever you want inside the event handler...

请务必您正在访问正确的用户界面,从后台线程控制,否则它会给你一个错误。

Be sure you're accessing correctly to UI controls from the background thread, otherwise it will give you an error.

这篇关于如何在后台最佳执行查询,以不冻结的应用程序(.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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