让计时器工作 [英] Getting a timer to work

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

问题描述

我对编程很陌生,但我在获取计时器时遇到问题

在visual studio.net工作


我'已经在表单上创建了一个计时器,启用它然后键入

以下代码(来自mdsn库,因为我认为这将是一个好的开始! )但没有任何反应: -

Imports System.Timers

公共类Form1

继承System.Windows.Forms.Form

#Region" Windows窗体设计器生成的代码

Public Sub New()

MyBase.New()

InitializeComponent()

End Sub

受保护的重载覆盖Sub Dispose(ByVal处理为

布尔值)

如果处置则

如果不是(组件什么都没有)那么

components.Dispose()

结束如果

结束如果

MyBase .Dispose(disposing)

End Sub

私有组件As System.ComponentModel.IContainer

''注意:以下过程是必需的Windows窗体

设计器

''可以使用Windows窗体设计器修改它。

''不要使用代码编辑器修改它。

Friend WithEvents Timer1 As System.Windows.Forms.Timer

< System.Diagnostics.DebuggerStepThrough()Private Sub

InitializeComponent()

Me.components = New System.ComponentModel.Container

Dim configurationAppSettings as

System.Configuration.AppSettingsReader = New

System.Configuration.AppSettingsReader

Me.Timer1 = New System.Windows.Forms.Timer( Me.components)

''

''Timer1

''

Me.Timer1.Enabled =

CType(configurationAppSettings.GetValue(" Timer1.En abled,

GetType(System.Boolean)),Boolean)

Me.Timer1 .Interval =

CType(configurationAppSettings.GetValue(Timer1.In terval,

GetType(System.Int32)),整数)

''

''Form1

''

Me.AutoScaleBaseSize = New System.Drawing.Size(5,13)

Me.ClientSize = New System.Drawing.Size(292,266)

Me.Name =" Form1"

Me.Text =" Form1

结束子

#End Region

结束班级

公共类Timer1

Public Shared Sub Main()

Dim aTimer As New System.Timers.Timer

AddHandler aTimer.Elapsed,AddressOf OnTimedEvent

aTimer.Interval = 2000

aTimer.Enabled = True

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

Console.ReadLine()

GC.KeepAlive(aTimer)

结束Sub

Private Shared Sub OnTimedEvent(ByVal source As Object,ByVal e

As

ElapsedEventArgs)

控制台。 WriteLine(Hello World!)

End Sub

结束课程

任何帮助将不胜感激。

谢谢

Paul

解决方案

" Hotrod2000" < pb ****** @ bmselland.comschrieb


我对编程很新,但是我在获取计时器方面遇到了问题

在visual studio.net中工作


我已在表单上创建了一个计时器,启用它然后键入

以下代码(来自mdsn库,因为我认为这将是一个好的开始!!!)但没有任何反应: -



- 没有你将下面的sub main设置为启动对象?

- 表格中的计时器没有用处,因为表格永远不会显示。

- 项目类型是否为控制台应用程序? Othwerwise你在控制台中看不到

输出(因为你看不到控制台)。

Armin

< blockquote class =post_quotes>
Public Class Timer1


Public Shared Sub Main()


Dim aTimer As New System。 Timers.Timer


AddHandler aTimer.Elapsed,AddressOf OnTimedEvent


aTimer.Interval = 2000

aTimer.Enabled = True


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

Console.ReadLine()


GC.KeepAlive(aTimer)

End Sub


私有共享子OnTimedEvent(ByVal源为对象,ByVal e
As

ElapsedEventArgs)

Console.WriteLine(" Hello World!")


End Sub

结束班



从您的代码中看起来像你试图附加一个不存在的Elapsed

事件。


尝试换行:

AddHandler aTimer.Elapsed,AddressOf OnTimedEvent


要:

AddHandler aTimer.Tick,AddressOf OnTimedEvent


我很惊讶VS甚至会毫无错误地编译项目。出于

好​​奇心,你有你提到的msdn链接吗?


" Matt F" < mf **************** @ nospam.nospamschrieb


从你的代码中,它看起来像你试图附加一个不存在的
Elapsed事件。


尝试更改行:

AddHandler aTimer .Elapsed,AddressOf OnTimedEvent


收件人:

AddHandler aTimer.Tick,AddressOf OnTimedEvent

我在很惊讶,VS甚至会毫无错误地编译项目。

出于好奇,你有你提到的msdn链接吗?



aTimer的类型是System.Timers.Timer,它有一个Elapsed事件。

Armin


I''m quite new to programming but I''m having problems getting a timer
to work in visual studio.net

I''ve created a timer on a form, enabled it and then typed the
following code (from the mdsn library as I thought this would be a
good start!!!) but nothing happens :-
Imports System.Timers
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ComponentModel.IContainer
''NOTE: The following procedure is required by the Windows Form
Designer
''It can be modified using the Windows Form Designer.
''Do not modify it using the code editor.
Friend WithEvents Timer1 As System.Windows.Forms.Timer
<System.Diagnostics.DebuggerStepThrough()Private Sub
InitializeComponent()
Me.components = New System.ComponentModel.Container
Dim configurationAppSettings As
System.Configuration.AppSettingsReader = New
System.Configuration.AppSettingsReader
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
''
''Timer1
''
Me.Timer1.Enabled =
CType(configurationAppSettings.GetValue("Timer1.En abled",
GetType(System.Boolean)), Boolean)
Me.Timer1.Interval =
CType(configurationAppSettings.GetValue("Timer1.In terval",
GetType(System.Int32)), Integer)
''
''Form1
''
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Name = "Form1"
Me.Text = "Form1"
End Sub
#End Region
End Class
Public Class Timer1
Public Shared Sub Main()
Dim aTimer As New System.Timers.Timer
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
aTimer.Interval = 2000
aTimer.Enabled = True
Console.WriteLine("Press the Enter key to exit the program.")
Console.ReadLine()
GC.KeepAlive(aTimer)
End Sub
Private Shared Sub OnTimedEvent(ByVal source As Object, ByVal e
As
ElapsedEventArgs)
Console.WriteLine("Hello World!")
End Sub
End Class
Any help would be greatly appreciated.
Thanks
Paul

解决方案

"Hotrod2000" <pb******@bmselland.comschrieb

I''m quite new to programming but I''m having problems getting a timer
to work in visual studio.net

I''ve created a timer on a form, enabled it and then typed the
following code (from the mdsn library as I thought this would be a
good start!!!) but nothing happens :-


- Did you set the sub main below as the startup object?
- The Timer in the Form has no purpose because the Form is never shown.
- Is the project type "console application"? Othwerwise you don''t see the
output in the console (because you don''t see the console).
Armin

Public Class Timer1
Public Shared Sub Main()
Dim aTimer As New System.Timers.Timer
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
aTimer.Interval = 2000
aTimer.Enabled = True
Console.WriteLine("Press the Enter key to exit the program.")
Console.ReadLine()
GC.KeepAlive(aTimer)
End Sub
Private Shared Sub OnTimedEvent(ByVal source As Object, ByVal e
As
ElapsedEventArgs)
Console.WriteLine("Hello World!")
End Sub
End Class



From your code, it looks like you''re attempting to attach to a Elapsed
event, which doesn''t exist.

Try changing the line:
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent

To:
AddHandler aTimer.Tick, AddressOf OnTimedEvent

I''m surprised the VS will even compile the project without errors. Out of
curiosity, do you have the msdn link that you mentioned?


"Matt F" <mf****************@nospam.nospamschrieb

From your code, it looks like you''re attempting to attach to a
Elapsed event, which doesn''t exist.

Try changing the line:
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent

To:
AddHandler aTimer.Tick, AddressOf OnTimedEvent

I''m surprised the VS will even compile the project without errors.
Out of curiosity, do you have the msdn link that you mentioned?

The type of aTimer is System.Timers.Timer which does have an Elapsed event.
Armin


这篇关于让计时器工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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