Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please check this for me 1

Status
Not open for further replies.

guitardave78

Programmer
Sep 5, 2001
1,294
GB
Can anyone tell me why this code does not do anything!!
Code:
Module Module1
    Private WithEvents Timer1 As New Timer
    Sub main()
        Timer1.Interval = 1000
        Timer1.Start()
        Console.ReadLine()
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim sProcesses() As System.Diagnostics.Process
        Dim sProcess As System.Diagnostics.Process
        Dim s As String
        MsgBox("tick")
        sProcesses = System.Diagnostics.Process.GetProcesses()
        s = ""
        s = vbCrLf & "Procss Info " & vbCrLf

        For Each sProcess In sProcesses
            s += sProcess.ProcessName() & vbCrLf
        Next
        Console.Write(s)
    End Sub
End Module

}...the bane of my life!
 
guitardave:

I pasted your code into a form and it worked fine.

Here are the results of the processes from my PC.
Code:
Procss Info 
whstart
MWSOEMON
wmiapsrv
sqlservr
svchost
lsass
msmdsrv
g2pre
g2svc
wdfmgr
services
qttask
hkcmd
GHOSTS~2
NMBgMonitor
svchost
smss
devenv
Test
mmc
svchost
inetinfo
g2comm
ctfmon
svchost
msnmsgr
defwatch
g2tray
mdm
msmsgs
vptray
svchost
winlogon
alg
WkUFind
sqlagent
WZQKPICK
OUTLOOK
iexplore
VBOrderClient
MAPISP32
osserver
rtvscan
explorer
isqlw
AcroRd32
csrss
spoolsv
oscmgr6
sqlmangr
System
MSGSYS
Idle
The program '[2844] Test.exe' has exited with code 0 (0x0).

I'm not sure about firing events from a module, but your code does work in a form.

I hope this helps.



Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.
 
This doesn't work as a console application. The tick event never gets fired. I'm guessing the reason has something to do with the fact that the timer control is part of the Windows.Forms class.

Below seems to work just fine, although you get the list instantly instead of a delay before each item:

Code:
Module Module1

   Sub main()
      OutputStuff()
      Console.ReadLine()
   End Sub

   Private Sub OutputStuff()
      Dim sProcesses() As System.Diagnostics.Process
      Dim sProcess As System.Diagnostics.Process
      Dim s As String
      'MsgBox("tick")
      sProcesses = System.Diagnostics.Process.GetProcesses()
      s = ""
      s = vbCrLf & "Procss Info " & vbCrLf

      For Each sProcess In sProcesses
         s += sProcess.ProcessName() & vbCrLf
      Next
      Console.Write(s)
   End Sub

End Module
 
Are you looking for a way of firing the method repeatedly, forever (endless loop), looking for a delay, firing until some condition exists or ??? If until a condition exists, a Do-While should work for you, endless loops are simple to create, but probably not what you're looking for. As for delays, I don't know of anything that won't suck up system resources since the timer won't work for you.
 
How about putting your Timer_1 Tick code within an infinite loop in another Sub, and then calling that sub on another thread. Then just have the thread sleep for 1 second at the end of each iteration of the loop?


Code:
Imports System.Threading

Module Module1

   Sub main()
      Dim timer as new Thread(AddressOf OutputStuff)
      timer.Start()
      Console.ReadLine()
   End Sub

   Private Sub OutputStuff()
      Do while true
          Dim sProcesses() As System.Diagnostics.Process
          Dim sProcess As System.Diagnostics.Process
          Dim s As String
          'MsgBox("tick")
          sProcesses = System.Diagnostics.Process.GetProcesses()
          s = ""
          s = vbCrLf & "Procss Info " & vbCrLf

          For Each sProcess In sProcesses
             s += sProcess.ProcessName() & vbCrLf
          Next
          Console.Write(s)
          Thread.CurrentThread.Sleep(1000)
        Loop
   End Sub
End Module
 
You can use a timer in a console app, just not the Windows.Forms timer. Use the System.Timers timer:

Dim t As System.Timers.Timer()


Here's an example from MSDN:

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



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top