Events in Console programs?

  • Thread starter Thread starter Terry Olsen
  • Start date Start date
T

Terry Olsen

Can console programs do events? Or are they strictly linear programs?
 
Okay...where can I find some example code? I haven't tried anything yet,
but I want to spawn a couple of threads from Sub Main and let them generate
the events. Do I need to keep Sub Main active (with a loop or something) to
keep the program from exiting?
 
Terry,
Okay...where can I find some example code?

I can't think of any that demonstrates exactly this, sorry.

I haven't tried anything yet,
but I want to spawn a couple of threads from Sub Main and let them generate
the events.

You generate events the same way regardless which type of executable
you're creating.

Do I need to keep Sub Main active (with a loop or something) to
keep the program from exiting?

Yes.



Mattias
 
Piece of cake - to demonstrate write a console application that spawns some
child threads. Immediately after spawning the threads - make a call to
Application.Run() which will start a new standard application message loop
(without a form object) and prevent your console application for immediately
terminating as would a typical console app.

Brain Patterson
http://dotnet.redeyepos.com
 
Terry,

You mean something as this?

\\\
Module Module1
Private running As Boolean = True
Private WithEvents tim As New System.Timers.Timer
Sub Main()
tim.Enabled = True
tim.Interval = 3000
Do While running
Console.Write("I am waiting" & vbCrLf)
Loop
End Sub
Private Sub tim_Elapsed(ByVal sender As Object, _
ByVal e As System.Timers.ElapsedEventArgs) Handles tim.Elapsed
running = False
End Sub
End Module
///
I hope this helps a little bit?

Cor
 
Terry Olsen said:
Okay...where can I find some example code? I haven't tried anything yet,
but I want to spawn a couple of threads from Sub Main and let them
generate the events. Do I need to keep Sub Main active (with a loop or
something) to keep the program from exiting?


Add a reference to 'System.Windows.Forms.dll', import the
'System.Windows.Forms' namespace, and call 'Application.Run' at the end of
your 'Sub Main'. Then you can call 'Application.ExitThread' to quit the
application.
 
Cor,

Cor Ligthert said:
Do While running
Console.Write("I am waiting" & vbCrLf)
Loop

Notice that this busy waiting loop will cause large CPU utilization :-(.
 
Herfried,
Notice that this busy waiting loop will cause large CPU utilization :-(.

Why, in this sample it is very busy showing lines to the console and that
takes CPU time, you cannot prevent that. However you can do beside it very
simple something else.

Maybe it is time you get a new computer.

It is not meant as a real application, however to show how to use an event
in a console application.

There can of course be build in any wait, to show the most simple one in
this case.
Threading.thread.sleep(1000).

However it is not a real application only to show that an event is possible.

Therefore I don't understand what you mean?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top