因sub main()函数 出现问题?

xue1234567890 2009-02-25 03:08:45
vb.net2003
在msdn 里有这样的例子...每5秒中弹出一个框...
(form里拖一个timer控件...)
程序:
Friend WithEvents Timer1 As System.Windows.Forms.Timer 'timer控件

Private Shared myTimer As New System.Windows.Forms.Timer
Private Shared alarmCounter As Integer = 1
Private Shared exitFlag As Boolean = False

' This is the method to run when the timer is raised.
Private Shared Sub TimerEventProcessor(ByVal myObject As Object, _
ByVal myEventArgs As EventArgs)
myTimer.Stop()

' Displays a message box asking whether to continue running the timer.
If MessageBox.Show("Continue running?", "Count is: " & alarmCounter, MessageBoxButtons.YesNo) = System.Windows.Forms.DialogResult.Yes Then
' Restarts the timer and increments the counter.
alarmCounter += 1
myTimer.Enabled = True
Else
' Stops the timer.
exitFlag = True
End If
End Sub

Public Shared Sub Main()
' Adds the event and the event handler for the method that will
' process the timer event to the timer.
AddHandler myTimer.Tick, AddressOf TimerEventProcessor

' Sets the timer interval to 5 seconds.
myTimer.Interval = 5000
myTimer.Start()

' Runs the timer, and raises the event.
While exitFlag = False
' Processes all the events in the queue.
Application.DoEvents()
End While

但是这样的话, 画面也不会显示, 想实现别的功能也不能实现...
好像每次被 sub main()函数初起化....
请帮我解释sub main()函数, 以及怎样做的话, 画面也能显示,然后也能实现每5秒钟弹出提示框...
...全文
181 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanlongwuhui 2009-02-27
  • 打赏
  • 举报
回复
Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
exitFlag = true
End Sub
yanlongwuhui 2009-02-26
  • 打赏
  • 举报
回复
理解上有点错误了,“关闭窗口, 就可以把整个程序结束(包括计时器)... ”,那就关闭窗口的时候,退出系统就可以了,如下:
Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
Application.Exit()
End Sub
yanlongwuhui 2009-02-26
  • 打赏
  • 举报
回复
试过上述代码,Form1_Load中没代码时,不存在“进程里还执行着”的问题。
xue1234567890 2009-02-26
  • 打赏
  • 举报
回复
Public Class Form1
Inherits System.Windows.Forms.Form

Private Shared myTimer As New System.Windows.Forms.Timer
Private Shared alarmCounter As Integer = 1
Private Shared exitFlag As Boolean = False

' This is the method to run when the timer is raised.
Private Shared Sub TimerEventProcessor(ByVal myObject As Object, _
ByVal myEventArgs As EventArgs)
myTimer.Stop()

' Displays a message box asking whether to continue running the timer.
If MessageBox.Show("Continue running?", "Count is: " & alarmCounter, MessageBoxButtons.YesNo) = System.Windows.Forms.DialogResult.Yes Then
' Restarts the timer and increments the counter.
alarmCounter += 1
myTimer.Enabled = True
Else
' Stops the timer.
exitFlag = True
End If

End Sub

Public Shared Sub Main()
' Adds the event and the event handler for the method that will
' process the timer event to the timer.

Dim objForm As New Form1
objForm.Show()

AddHandler myTimer.Tick, AddressOf TimerEventProcessor

' Sets the timer interval to 5 seconds.
myTimer.Interval = 5000
myTimer.Start()


' Runs the timer, and raises the event.
While exitFlag = False
' Processes all the events in the queue.
Application.DoEvents()
End While

End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
End Class

执行顺序是sub main()函数,然后Form1_Load, 上面有人说过 sub main()平常方法, 我想不是.,..
我想实现的是执行后, 关闭窗口, 就可以把整个程序结束(包括计时器)...

需要求助..
yanlongwuhui 2009-02-26
  • 打赏
  • 举报
回复
一般都是在模块中写sub main函数,在main中调用Form1,启动选择sub main启动。
代码不很多的话,你贴上来看下
xue1234567890 2009-02-26
  • 打赏
  • 举报
回复
我的程序是在窗体上...

我的程序中还有 Form1_Load方法..
通过调试, 先调用sub main函数,然后再调用Form1_Load方法....
如果sub main函数里的都注释的话,调用sub main函数之后不会调用Form1_Load方法。。。

这个能解释一下吗? 他们之间有什么关系?
xue1234567890 2009-02-26
  • 打赏
  • 举报
回复
up
xue1234567890 2009-02-26
  • 打赏
  • 举报
回复
按照15楼的说法做的话,关闭窗口之后,计时器也不运行了...但是进程里还继续运行着...
如果每次执行, 进程里一直保留的话, 这个程序是无法想像的...

请高手指教...
xue1234567890 2009-02-26
  • 打赏
  • 举报
回复
等答案...
xue1234567890 2009-02-25
  • 打赏
  • 举报
回复
不行... 进程里还执行着..
yanlongwuhui 2009-02-25
  • 打赏
  • 举报
回复
在 End While的后面再加一句试试:
Application.Exit()
xingyichuan 2009-02-25
  • 打赏
  • 举报
回复
小弟也是才学习.NET,有理解错误的地方请高人指点。
对的,关闭程序就关闭了这个程序。其实在你关闭对话框后,已经将计时器关闭了。可以加个短点看看myTimer.Enabled值。
xue1234567890 2009-02-25
  • 打赏
  • 举报
回复
一般程序都不是关闭form会关闭整个程序吗?
就是要想关闭整个form...
xingyichuan 2009-02-25
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xue1234567890 的回复:]
补充: 当我关闭程序时,计时器也不要再运行。。。怎么实现?
[/Quote]
我不太清楚楼主的意思,是指关闭对话框还是关闭整个Form?
xingyichuan 2009-02-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xue1234567890 的回复:]
我的程序是在窗体上...

我的程序中还有 Form1_Load方法..
通过调试, 先调用sub main函数,然后再调用Form1_Load方法....
如果sub main函数里的都注释的话,不会调用Form1_Load方法。。。

这个能解释一下吗? 他们之间有什么关系?

[/Quote]

Form1_Load 好像是读取窗体这个时间,sub main好像只是一般的过程。
xingyichuan 2009-02-25
  • 打赏
  • 举报
回复
我试了下在窗体加载的时间中加楼主sub main的函数结果是可以的,楼主可以试试:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

AddHandler myTimer.Tick, AddressOf TimerEventProcessor

' Sets the timer interval to 5 seconds.
myTimer.Interval = 5
myTimer.Start()

' Runs the timer, and raises the event.
While exitFlag = False
' Processes all the events in the queue.
Application.DoEvents()
End While
End Sub
xue1234567890 2009-02-25
  • 打赏
  • 举报
回复
补充: 当我关闭程序时,计时器也不要再运行。。。怎么实现?
xue1234567890 2009-02-25
  • 打赏
  • 举报
回复
我的程序是在窗体上...

我的程序中还有 Form1_Load方法..
通过调试, 先调用sub main函数,然后再调用Form1_Load方法....
如果sub main函数里的都注释的话,不会调用Form1_Load方法。。。

这个能解释一下吗? 他们之间有什么关系?
yanlongwuhui 2009-02-25
  • 打赏
  • 举报
回复
你的sub main函数应该是在窗体上,不是在模块中的吧。
假设你的窗体名为Form1,则在 AddHandler myTimer.Tick, AddressOf TimerEventProcessor 前面加上以下代码试试:
Dim objForm As New Form1
objForm.Show()

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧