Family said:
You can compare almost anything in VB. Whether it makes sense is
another story. With "Option Strict On" you are protected from most
mistakes.
For clarification, ConsoleKeyInfo is a struct. For the comparison I
would use a ConsoleKey enumeration item. That will make the code more
readable. Actually the code could be written like this: (Forgive me as
I don't know what key 27 is equal).
Escape
Dim k As ConsoleKey
While (True)
k = Console.ReadKey(True).Key
If (k = ConsoleKey.Q) Then
Exit While
End If
End While
In other words, you don't even need to check the key available. ReadKey
will block.
Well, thats a different need. This is for an threaded application so
the cooperative non-blocking keyboard loop is used under this
considerations. Coupled with the other post regarding threads and
callbacks, the completed example for our SDK was done:
' file: Example-CallBack2.vb
' About: Example of Wildcat Callback
Option Strict On
Option Explicit On
Imports System
Imports System.Threading
Imports System.Runtime.InteropServices
Imports Wildcat.Net.Server
Module Module1
sub BeepStartUp()
Console.Beep(1000, 200) : Console.Beep(2000, 50)
end sub
sub BeepShutDown()
Console.Beep(2000, 200) : Console.Beep(1000, 50)
end sub
Function GetKey() as integer
return console.ReadKey(true).key
end function
Function KeyPressed() as boolean
return Console.KeyAvailable()
end function
Delegate Function cbWildcatDelegate( _
ByVal userdata As Integer, _
ByVal msg As IntPtr) as Long
Function DoCallBack( _
ByVal userdata As Integer, _
ByVal msg As IntPtr) as Long
try
Dim cmsg As TChannelMessage
cmsg.Channel = Marshal.ReadInt32(msg, 0)
cmsg.SenderId = Marshal.ReadInt32(msg, 4)
cmsg.UserData = Marshal.ReadInt16(msg, 8)
cmsg.DataSize = Marshal.ReadInt16(msg, 10)
Console.WriteLine("CB: Ch: {0} sid: {1} ud: {2} ds: {3}", _
cmsg.Channel, _
cmsg.SenderId, _
cmsg.UserData, _
cmsg.DataSize)
Catch ex As Exception
Console.WriteLine("-------------------------------------")
Console.WriteLine("Exception: {0}", ex.Message)
Console.WriteLine("{0}", ex.StackTrace)
Console.WriteLine("-------------------------------------")
end try
return 0
end function
Public Function PrepareCallback( _
ByVal cbproc As cbWildcatDelegate, _
ByVal userdata As Integer) As Boolean
SetupWildcatCallback(cbProc, userdata)
End Function
'-------------------------------------------
' MAIN
'-------------------------------------------
Sub Main()
BeepStartUp()
' Find/Connect to WAN/LAN server
WildcatServerConnect(0)
Console.Writeline("- Connected to: {0}",GetConnectedServer())
' Creates thread
WildcatServerCreateContext()
' adds callback
PrepareCallBack(AddressOf DoCallBack, 0)
' listen to some channels
dim ChatChannel as Integer = OpenChannel("chat.0")
dim SysPageChannel as Integer = OpenChannel("system.page")
dim SysEventChannel as Integer = OpenChannel("system.event")
' cooperative keyboard loop
while (true)
if KeyPressed() AndAlso (GetKey() = 27) then
exit while
end if
Thread.Sleep(75)
end while
WildcatServerDeleteContext()
Console.Writeline("<CLICK>")
BeepShutDown()
End Sub
End Module
--