程序计时器(非图形) [英] Timer for program (non graphic)

查看:58
本文介绍了程序计时器(非图形)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我写了一个程序来检查文件,当它们进来时将这些文件移动到

正确的文件夹。一切正常。但是我总是需要手动启动该程序。我一直试图让程序

(没有用户界面)继续运行,让它每隔几分钟执行一次所有内容(取决于定时器的设置方式) )。


谁能告诉我如何在程序中获得计时器,以便程序保持运行?
?我可以添加一个计时器,但是当所有东西都运行时,程序就会退出。我不想让它关闭。


谢谢

Joris

Hi,

I have wrote a program that checks for files and moves these files to the
correct folders when they come in. Everything works fine. However I always
have to start that program manually. I have been trying to make the program
(wich has no UI) to keep running and let it execute everything once every
few minutes (depending on how the timer is set).

Can anyone tell me how I can get a timer in the program so the program keeps
running? I can add a timer, but the program just quits when everything has
run. I don''t want it to close.

Thanks
Joris

推荐答案



" Joris De Groote" < jo ************ @ skynet.bewrote in message

news:%2 **************** @ TK2MSFTNGP05.phx.gbl ...

"Joris De Groote" <jo************@skynet.bewrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...




我写了一个检查文件并移动这些文件的程序文件到

正确的文件夹进来。一切正常。但是我总是需要手动启动该程序。我一直试图让

程序(没有UI)继续运行,让它每隔几分钟执行一次

(取决于定时器的设置方式) )。
Hi,

I have wrote a program that checks for files and moves these files to the
correct folders when they come in. Everything works fine. However I always
have to start that program manually. I have been trying to make the
program (wich has no UI) to keep running and let it execute everything
once every few minutes (depending on how the timer is set).



您使用的是控制台应用程序吗?

Are you using a Console Application?


>

可以有谁告诉我如何在程序中获得计时器,以便程序

继续运行?我可以添加一个计时器,但程序刚退出时

一切都运行了。我不想让它关闭。
>
Can anyone tell me how I can get a timer in the program so the program
keeps running? I can add a timer, but the program just quits when
everything has run. I don''t want it to close.



您正在使用System.Timer命名空间?


此示例来自VS的VS帮助。


进口系统

进口System.Timers


公共舱Timer1


公开Shared Sub Main()

''通常,计时器是在类级别声明的,所以

''表示方法不会超出范围结束。

''在这个例子中,仅当Main

''正在执行时才需要计时器。但是,KeepAlive必须在Main的

''结束时使用,以防止JIT编译器允许

''积极垃圾收集在Main之前发生

''结束。

Dim aTimer As New System.Timers.Timer()


''为计时器连接Elapsed事件。

AddHandler aTimer.Elapsed,AddressOf OnTimedEvent


''将间隔设置为2秒(2000毫秒)。

aTimer。间隔= 2000

aTimer.Enabled = True

Console.WriteLine(按Enter键退出程序。)

Console.ReadLine()


''让计时器保持活着直到Main结束。

GC.KeepAlive(aTimer)
结束子


''指定当Elapsed事件为
''筹集时你想要发生什么。

Private Shared Sub OnTimedEvent(source As Object,e As ElapsedEventArgs)

Console.WriteLine(" Hello World!")

End Sub

结束类


您知道FileSystemWatcher将检查文件的位置

进入文件夹并发起活动。

http://www.google.com/search?hl=en&q...=Google+Search

You are using the System.Timer namespace?

This example came from VS Help for VB.

Imports System
Imports System.Timers

Public Class Timer1

Public Shared Sub Main()
'' Normally, the timer is declared at the class level, so
'' that it doesn''t go out of scope when the method ends.
'' In this example, the timer is needed only while Main
'' is executing. However, KeepAlive must be used at the
'' end of Main, to prevent the JIT compiler from allowing
'' aggressive garbage collection to occur before Main
'' ends.
Dim aTimer As New System.Timers.Timer()

'' Hook up the Elapsed event for the timer.
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent

'' Set the Interval to 2 seconds (2000 milliseconds).
aTimer.Interval = 2000
aTimer.Enabled = True

Console.WriteLine("Press the Enter key to exit the program.")
Console.ReadLine()

'' Keep the timer alive until the end of Main.
GC.KeepAlive(aTimer)
End Sub

'' Specify what you want to happen when the Elapsed event is
'' raised.
Private Shared Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
Console.WriteLine("Hello World!")
End Sub
End Class

You do know about the FileSystemWatcher where it will check for a file
coming in to a folder and fire an event.

http://www.google.com/search?hl=en&q...=Google+Search


Joris,


这需要是一个服务而不是控制台应用程序。


Cor


" Joris De Groote" < jo ************ @ skynet.beschreef在bericht

新闻:%2 **************** @ TK2MSFTNGP05.phx.gbl ...
Joris,

This needs to be a "Service" instead of a console application.

Cor

"Joris De Groote" <jo************@skynet.beschreef in bericht
news:%2****************@TK2MSFTNGP05.phx.gbl...




我写了一个检查文件并移动这些文件的程序文件到

正确的文件夹进来。一切正常。但是我总是需要手动启动该程序。我一直试图让

程序(没有UI)继续运行,让它每隔几分钟执行一次

(取决于定时器的设置方式) )。


谁能告诉我如何在程序中获得计时器,以便程序

继续运行?我可以添加一个计时器,但程序刚退出时

一切都运行了。我不想让它关闭。


谢谢

Joris
Hi,

I have wrote a program that checks for files and moves these files to the
correct folders when they come in. Everything works fine. However I always
have to start that program manually. I have been trying to make the
program (wich has no UI) to keep running and let it execute everything
once every few minutes (depending on how the timer is set).

Can anyone tell me how I can get a timer in the program so the program
keeps running? I can add a timer, but the program just quits when
everything has run. I don''t want it to close.

Thanks
Joris





" Cor Ligthert [MVP]" < no ************ @ planet.nlwrote in message

news:Om ************** @ TK2MSFTNGP02.phx .gbl ...

"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:Om**************@TK2MSFTNGP02.phx.gbl...

Joris,


这需要是服务而不是控制台应用程序。
Joris,

This needs to be a "Service" instead of a console application.



为什么?人们当然可以构建一个可以保持运行的控制台应用程序,并且24/7 * 365运行,直到有人告诉它关闭,并且它可以做任何事情。

a服务应用程序可以关于OP的解决方案。

Why? One can certainly build a console application that will stay up and
running 24/7*365, until one tells it to shutdown, and it do everything that
a Service application can do in regards to the OP''s solution.


这篇关于程序计时器(非图形)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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