什么时候线程比计时器更好? [英] when is threading better than a timer?

查看:65
本文介绍了什么时候线程比计时器更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我已经这么做了好几年了,但现在我只是因为

这个想法因为我开始使用DotNet而一直处于我的脑后。 。


我的WinForms应用程序每60秒查询一次数据库。我设置了一个单例

类,它在应用程序启动时实例化,然后启动计时器。我的

问题是,在单独的

线程上运行此检查过程并让它使用自己的数据库连接会更好吗?如果没有明确的是或

否,会有什么情况影响答案?


TIA,

鲍勃

Okay, I''ve done this for years but now I''m going to question it just because
this idea has been at the back of my head since I started using DotNet...

My WinForms app queries a database every 60 seconds. I set up a singleton
class that''s instantiated when the app starts, and that starts a timer. My
question is, would it be better to run this checking process on a separate
thread and have it use its own DB connection? If there isn''t a clear yes or
no, what circumstances affect the answer?

TIA,
Bob

推荐答案

Bob,


在我看来,就像这样,永远当你不需要穿线时'$

使用它,操作系统应该关注线程,这样只需要额外的处理时间。额外的线程是非常有用的,当你可以让2个进程同时进行
时,它们都有等待时间,所以他们可以继续下去

而另一个是等候。一个非常好的例子就是下载。


和你的样本,当你使用计时器时,你的应用程序什么都不做,而不是
等待一个事件。其余的都停止了。你想让它两个

次等待活动吗?


但是只是我的想法。


Cor

" Bob" < no *** @ nowhere.com>
Bob,

In my opinion it is like this, for ever when you do not need threading don''t
use it, the OS should keep eyes on the threads so it will be only extra
processing time. Extra threads are very usefull when you can let 2 processes
go in the same time, which have both wait moments, so they can go both go on
while the other is waiting. A very good example for that is downloading.

And your sample, when you use a timer, your application does nothing than
wait on an event. For the rest it is stopped. Do you want to let it two
times wait on events?

However just my thought.

Cor
"Bob" <no***@nowhere.com>
好的,我已经这么做了很多年了但是现在我要问它只是
因为
这个自从我开始使用DotNet以来,这个想法一直在我的脑后......

我的WinForms应用程序每60秒查询一次数据库。我设置了一个单独的类,它在应用程序启动时实例化,并启动计时器。我的问题是,在一个单独的线程上运行这个检查过程并让它使用自己的数据库连接会更好吗?如果没有明确的是

否,会有什么情况影响答案?

TIA,
Bob
Okay, I''ve done this for years but now I''m going to question it just
because
this idea has been at the back of my head since I started using DotNet...

My WinForms app queries a database every 60 seconds. I set up a singleton
class that''s instantiated when the app starts, and that starts a timer. My
question is, would it be better to run this checking process on a separate
thread and have it use its own DB connection? If there isn''t a clear yes
or
no, what circumstances affect the answer?

TIA,
Bob



Bob,

如果你有一个长时间运行的过程(半分钟或更长时间)我会

推荐一个单独的线。通过长时间运行,我的意思是如果需要足够长的时间

用户将开始认为你的应用程序停止响应(挂起)。


如果数据库检查需要几分之一秒我会留下它作为一个

Windows计时器。


请记住,.NET中有三个不同的计时器对象。其中一个

两个可能更合适,MSDN杂志中的以下文章解释了

.NET中的三个计时器对象之间的区别。何时使用每个。

http://msdn.microsoft.com/msdnmag/is...T/default.aspx

http://msdn.microsoft.com/msdnmag/is...3/default.aspx


以上文章还讨论了&每个计时器如何与

线程交互。


希望这有帮助

Jay

希望这有助于

Jay


" Bob" <无*** @ nowhere.com>在消息中写道

新闻:ei ************** @ TK2MSFTNGP11.phx.gbl ...
Bob,
If you have a long running process (half a minute or longer) I would
recommend a separate thread. By long running, I mean if it takes long enough
that the user will start to think your app stopped responding (hung).

If the database check takes fractions of a second I would leave it as a
Windows Timer.

Remember there are three distinct timer objects in .NET. One of the other
two may be more appropriate, the following articles in MSDN Magazine explain
the difference between the three timer objects in .NET & when to use each.

http://msdn.microsoft.com/msdnmag/is...T/default.aspx

http://msdn.microsoft.com/msdnmag/is...3/default.aspx

The above articles also discusses if & how each timer interacts with
threading.

Hope this helps
Jay
Hope this helps
Jay

"Bob" <no***@nowhere.com> wrote in message
news:ei**************@TK2MSFTNGP11.phx.gbl...
好的,我是'我已经这么做了多年,但现在我只是要问它
因为自从我开始使用DotNet以来,这个想法一直在我的脑后......
我的WinForms应用程序每隔60秒查询一次数据库。我设置了一个单独的类,它在应用程序启动时实例化,并启动计时器。我的问题是,在一个单独的线程上运行这个检查过程并让它使用自己的数据库连接会更好吗?如果没有明确的是

否,会有什么情况影响答案?

TIA,
Bob
Okay, I''ve done this for years but now I''m going to question it just
because
this idea has been at the back of my head since I started using DotNet...

My WinForms app queries a database every 60 seconds. I set up a singleton
class that''s instantiated when the app starts, and that starts a timer. My
question is, would it be better to run this checking process on a separate
thread and have it use its own DB connection? If there isn''t a clear yes
or
no, what circumstances affect the answer?

TIA,
Bob



它是毫秒。看起来我正在使用正确的Timer类,因为我希望它与

用户界面相互接合并尊重它。非常好的链接,谢谢。


Bob


Jay B. Harlow [MVP - Outlook]" < JA ************ @ msn.com>在消息中写道

新闻:OF **************** @ TK2MSFTNGP10.phx.gbl ...
It''s milliseconds. Looks like I was using the right Timer class, too
(System.Windows.Forms.Timer), since I wanted it to mesh with and respect the
UI. Very good links, thank you.

Bob

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OF****************@TK2MSFTNGP10.phx.gbl...
Bob,<如果你有一个长时间运行的过程(半分钟或更长时间),我会建议一个单独的线程。通过长时间运行,我的意思是,如果花费足够长的
,用户将开始认为你的应用程序停止响应(挂起)。

如果数据库检查需要几分之一秒钟,我会离开它作为一个Windows计时器。

请记住,.NET中有三个不同的计时器对象。其中两个可能更合适,MSDN杂志中的以下文章
解释了.NET& S中三个计时器对象之间的区别。何时使用每个。

http://msdn.microsoft.com/msdnmag/is...T/default.aspx

http://msdn.microsoft.com/msdnmag/is...3/default.aspx <上面的文章还讨论了如果&每个计时器如何与线程交互。

希望这有帮助
Jay

希望这会有所帮助
Jay
鲍勃 <无*** @ nowhere.com>在消息中写道
新闻:ei ************** @ TK2MSFTNGP11.phx.gbl ...
Bob,
If you have a long running process (half a minute or longer) I would
recommend a separate thread. By long running, I mean if it takes long enough that the user will start to think your app stopped responding (hung).

If the database check takes fractions of a second I would leave it as a
Windows Timer.

Remember there are three distinct timer objects in .NET. One of the other
two may be more appropriate, the following articles in MSDN Magazine explain the difference between the three timer objects in .NET & when to use each.

http://msdn.microsoft.com/msdnmag/is...T/default.aspx

http://msdn.microsoft.com/msdnmag/is...3/default.aspx

The above articles also discusses if & how each timer interacts with
threading.

Hope this helps
Jay
Hope this helps
Jay

"Bob" <no***@nowhere.com> wrote in message
news:ei**************@TK2MSFTNGP11.phx.gbl...
好的,我这样做了几年,但现在我要问它只是
因为自从我开始使用
DotNet以来,这个想法一直在我的脑后......
我的WinForms应用程序每60秒查询一次数据库。我设置了一个
单例类,它在应用程序启动时实例化,并启动一个计时器。
我的问题是,在
单独的线程上运行此检查过程并让它使用自己的数据库连接会更好吗?如果没有明确的是

否,会有什么情况影响答案?

TIA,
Bob
Okay, I''ve done this for years but now I''m going to question it just
because
this idea has been at the back of my head since I started using DotNet...
My WinForms app queries a database every 60 seconds. I set up a singleton class that''s instantiated when the app starts, and that starts a timer. My question is, would it be better to run this checking process on a separate thread and have it use its own DB connection? If there isn''t a clear yes
or
no, what circumstances affect the answer?

TIA,
Bob




这篇关于什么时候线程比计时器更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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