Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CTRL-C & Console?

21 views
Skip to first unread message

Mike Curry

unread,
May 28, 2003, 7:49:31 AM5/28/03
to
Is there anyway to capture when a user presses CTRL-C in a VB.Net console
client?


Cowboy (Gregory A. Beamer)

unread,
May 28, 2003, 9:31:15 AM5/28/03
to
Short answer: No! The module does not allow you to catch anything.

If you want to do clean up when Control-C is pressed, you can put your
functionality in a class and use finalize() method to clean things up. Or
create a deconstructor. You can also have this in the start up by adding a
"startup" class, putting the Main() in this class and using its methods:

Public Class StartUpClass
Public Shared Sub Main()
System.Console.WriteLine("Just some text to illustrate.")
System.Console.Read() 'Stops output
End Sub
End Class

The difficulty here is the Module. A module is simply a shared class
internally, but it greatly restricts you in many ways. It was thrown in,
IMO, for VB developers that routinely used .bas files for global settings. I
am not particularly fond of the Module as the Starting point

If you want to stop the Control+C, you will have to kludge this a bit. In
VB6, I used to make formless standard EXE projects to access the methods
necessary to abort. Have not tried similar in .NET.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
Author: ADO.NET and XML: ASP.NET on the Edge

****************************************************************************
****
Think Outside the Box!
****************************************************************************
****
"Mike Curry" <mcurr...@rogers.com> wrote in message
news:fn1Ba.263267$M81....@news02.bloor.is.net.cable.rogers.com...

Mark Hurd

unread,
May 28, 2003, 10:38:22 AM5/28/03
to
Mike Curry wrote:
> Is there anyway to capture when a user presses CTRL-C in a VB.Net console
> client?

You're in luck. Someone else has just posted a C# solution to this in one of
these newsgroups (sorry I didn't keep a reference to the post) and I've
converted it to VB.NET today:

Option Explicit On
Option Strict On

Imports System

Namespace ConsoleCtrlSample

Class ConsoleCtrl
Implements IDisposable

Public Enum ConsoleEvent
CTRL_C = 0 ' From wincom.h
CTRL_BREAK = 1
CTRL_CLOSE = 2
CTRL_LOGOFF = 5
CTRL_SHUTDOWN = 6
End Enum

' Handler to be called when a console event occurs.
Public Delegate Sub ControlEventHandler _
(ByVal TheEvent As ConsoleEvent)

' Event fired when a console event occurs
Public Event ControlEvent As ControlEventHandler

Private EventHandler As ControlEventHandler

Public Sub New()

' save this to a private var so the GC doesn't collect it...
EventHandler = AddressOf Handler
SetConsoleCtrlHandler(EventHandler, True)
End Sub

Protected Overrides Sub Finalize()
Dispose(False)
End Sub

Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
End Sub

Public Sub Dispose(ByVal Disposing As Boolean)

If Not (EventHandler Is Nothing) Then
SetConsoleCtrlHandler(EventHandler, False)
EventHandler = Nothing
End If
End Sub

Private Sub Handler(ByVal TheEvent As ConsoleEvent)
If Not (EventHandler Is Nothing) Then
RaiseEvent ControlEvent(TheEvent)
End If
End Sub

Private Declare Function SetConsoleCtrlHandler _
Lib "kernel32.dll" (ByVal e As ControlEventHandler, _
ByVal Add As Boolean) As Boolean
End Class

Class CtrlTest
Public Shared Sub MyHandler(ByVal ConsoleEvent _
As ConsoleCtrl.ConsoleEvent)
Console.WriteLine("ConsoleEvent: {0}", ConsoleEvent)
End Sub

Public Shared Sub Main()
Dim cc As New ConsoleCtrl()
AddHandler cc.ControlEvent, New _
ConsoleCtrl.ControlEventHandler(AddressOf MyHandler)

Console.WriteLine("Enter 'E' to exit")

Do
Loop Until Console.ReadLine() = "E"
End Sub
End Class
End Namespace

I'm sure VB.NET's Event support means that the delegate and AddHandler
manipulation could be simplified (probably removed) but this worked for my
purposes.

Regards,
Mark Hurd, B.Sc.(Ma.) (Hons.)


Thomas Scheidegger [MVP]

unread,
May 28, 2003, 2:23:53 PM5/28/03
to
0 new messages