简体   繁体   English

不断检查SQL Server连接的标准方法是什么?

[英]What is the standard way to constantly check SQL Server connection?

In a WPF application I use LINQ to SQL queries inside try - catch constructions to process exceptions, in case there is something wrong with DB server connection. 在WPF应用程序中,我在try-catch构造中使用LINQ to SQL查询来处理异常,以防DB服务器连接出现问题。

The problem is - I have some queries executing on a timer-polling basis. 问题是 - 我有一些查询在计时器轮询的基础上执行。 So, if connection fails, I have numerous long queries attempts and UI is freezing. 因此,如果连接失败,我会进行大量长查询尝试,并且UI处于冻结状态。

What is the standard way out? 标准出路是什么? I would like to have easy lightweighted way of constant checking if db connection is OK and then do all the stuff with all my queries. 我希望有一个简单的轻量级方法来不断检查数据库连接是否正常,然后用我所有的查询做所有的事情。

run it in a backgroundworker thread, or a separate thread entirely. 在backgroundworker线程中运行它,或者完全在单独的线程中运行它。

Work should not be done on the UI thread, unless it directly pertains to the UI itself. 不应该在UI线程上完成工作,除非它直接与UI本身相关。 BackgroundWorker is an object that makes threading stuff like this really easy. BackgroundWorker是一个使这样的线程非常简单的对象。 For a tutorial see this entry . 有关教程,请参阅此条目 It explains what Background worker does and how to use it. 它解释了后台工作人员的工作以及如何使用它。

If you do work on the UI thread, you are stealing CPU cycles away from the UI causing it to feel slow to the user. 如果您在UI线程上工作,则会从UI中窃取CPU周期,从而导致用户感觉很慢。 Running the work on it's own thread and then updating the UI is the best way to do it. 在它自己的线程上运行工作, 然后更新UI是最好的方法。 That way the user can do other stuff and the UI responds properly. 这样,用户可以做其他事情并且UI正确响应。

Why not move your polling queries onto their own thread so they won't freeze the UI? 为什么不将轮询查询移动到他们自己的线程上,这样他们就不会冻结UI? Then they can raise a message or event to your main UI thread and you can handle it there (pop up an error message, etc). 然后,他们可以向您的主UI线程发送消息或事件,您可以在那里处理它(弹出错误消息等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM