By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,214 Members | 2,072 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,214 IT Pros & Developers. It's quick & easy.

Events in Console programs?

P: n/a
Can console programs do events? Or are they strictly linear programs?
Nov 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
>Can console programs do events?

Yes

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2

P: n/a
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?

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:ev****************@TK2MSFTNGP09.phx.gbl...
Can console programs do events?


Yes

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 21 '05 #3

P: n/a
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #4

P: n/a
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 Olsen" <to******@hotmail.com> wrote in message
news:OK****************@TK2MSFTNGP09.phx.gbl...
Can console programs do events? Or are they strictly linear programs?

Nov 21 '05 #5

P: n/a
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
Nov 21 '05 #6

P: n/a
"Terry Olsen" <to******@hotmail.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #7

P: n/a
Cor,

"Cor Ligthert" <no************@planet.nl> schrieb:
Do While running
Console.Write("I am waiting" & vbCrLf)
Loop


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

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #8

P: n/a
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

Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.