ThreadBoard ArchivesSite FeaturesActiveworlds SupportHistoric Archives |
VB.NET Timers & AwSdk (Sdk)
VB.NET Timers & AwSdk // Sdkr i c h a r dMar 31, 2005, 6:27pm
Why does this not work properly?
This is only a sample for a real project, the timer works. The timer writes saying hello world but the bot does not say hello world nor does it write said hello world to the console. Any attempt to call the SDK from the timer cause it to exit the sub? Events all work whats up wtih the timer? Imports System.Timers Imports AWSDKLib3 Imports AWSDKLib3.AW_SDK_CONSTANTS Module Module1 Dim sdk As AwSdk3 Sub Main() Dim aTimer As New System.Timers.Timer(100) AddHandler aTimer.Elapsed, AddressOf OnTimedEvent sdk = New AwSdk3 sdk.aw_init(AW_BUILD) sdk.QuickStart('logininfo here) sdk.aw_state_change() sdk.aw_say("Hi!") aTimer.Enabled = True aTimer.AutoReset = True Do sdk.aw_wait(-1) Loop End Sub Private Sub OnTimedEvent(ByVal source As Object, ByVal e As ElapsedEventArgs) Console.WriteLine("Saying Hello World!...") sdk.aw_say("Hello World!") Console.WriteLine("Said Hello World!...") End Sub End Module scifairMar 31, 2005, 8:09pm
Hi Richard-
I'm going to guess that it's because the SDK is in an aw_wait when you're trying to do the say command, and it seems that the wrapper or SDK is synchronizing SDK calls. Try changing that aw_wait in your main loop to use a non-infinite wait time (preferably one that is relatively short). Are you certain the sub is exiting, and not blocking (waiting) on the aw_say call? Is it throwing an exception? If you're going to have a multi-threaded app (yours is multi-threaded already because you are using a timer) you should be synchronizing to make it thread-safe yourself anyway. You could do something like using SyncLock(sdk) around any groups of SDK calls (and do something analagous around any data or other resources that might be shared between threads). Of course, this is only my guess as to what's happening; I've never used the particular wrapper you are using. -- Rich [View Quote] r i c h a r dMar 31, 2005, 9:15pm
Well i used aw_wait -1 because thats what they used in the C++ aw sdk
examples which are designed for console applications i just modded it to be VB.NET.. Theres no exceptions it just aborts the sub the moment it hits AwSdk? Ill have a play.. strike rapierMar 31, 2005, 10:27pm
scifairMar 31, 2005, 11:24pm
The timer he's using uses an event to signal, specifically the Elapsed event
(this is not a system event). Either way, the handler would run in a different thread from the main loop (in this case a system thread). In fact his code uses at least 3 threads, one in the main execution thread, one to operate the timer, and one for the timer's callback (plus however many the SDK adds). -- Rich [View Quote] scifairMar 31, 2005, 11:26pm
strike rapierMar 31, 2005, 11:41pm
If its the AX timer I am thinking about...
The timer is a registered timer on the OS, which automatically calls it based on the clock frequency of the motherboard, the os SYSTEM thread then will call the defined function as required. The handler for the sdk events and timer is just a defined code entry point called by a pointer, or in the case of AX, an inherited event, based on when aw_wait is called and each message is dispatched in turn. - MR [View Quote] r i c h a r dApr 1, 2005, 12:20am
This is Visual Studio 2003 using .NET Framework 1.1
Whats the difference between: System.Timers.Timer and System.Threading.Timer strike rapierApr 1, 2005, 12:24am
Threading.timer is safe for multithreading applications.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemthreadingtimerclasstopic.asp Changing its attributes and such i would imagine. - MR [View Quote] r i c h a r dApr 1, 2005, 12:37am
I stuck in break points and I would assume if it was blocking it would
highlight the Sdk.aw_say line before runnning that line which it doesn't it does the console.writeline i hit play then it rights to the console but never raises the line involving the sdk nor anything after it. [View Quote] scifairApr 1, 2005, 1:52am
Sounds more like the thread's going to sleep to me. If you let it run, does
it print that first line each time the timer elapses, or does it only enter the handler and print once? While your debugger is running, turn on Debug->Windows->Threads. If the handler is entered repeatedly (and if you have a breakpoint there), what happens in your Threads window? -- Rich [View Quote] strike rapierApr 1, 2005, 2:03am
I don't use VB.NET (I prefer the 'real' RAD VB and C++), but does your timer
usually work in this situation? I see an infinate loop with the AW waits, probably a WFSO in there to do the wait duration, and your timer, at which point the AWSDK probably has a set of threads running for its winsock handlers. Try seing if it freezes on any other SDK events. - MR [View Quote] scifairApr 1, 2005, 2:08am
When using the System.Timers.Timer timer (as Richard is using), the
application's process gets handles to 2 new threads, one when the timer starts, and one the first time the timer elapses. It gets no new handles to system timer objects or any other system objects, as far as I can tell. Maybe there is a system timer being used that is buried somewhere in a library such that the process never gets a handle for it, but it's much more likely that the timer is implemented as a thread sleep on that first timer-related thread. -- Rich [View Quote] andrasApr 1, 2005, 7:58am
[View Quote]
> Why does this not work properly?
> > This is only a sample for a real project, the timer works. The timer writes > saying hello world but the bot does not say hello world nor does it write > said hello world to the console. Any attempt to call the SDK from the timer > cause it to exit the sub? Events all work whats up wtih the timer? > I'm not a VB expert but I know some about the SDK itself :) As Rich (scifair) found properly, you were calling the aw_wait(-1) which means it will wait forever for an event. While you are INSIDE the SDK handling, your timer kicks in and you try to invoke an SDK routine . As a rule of thumb - if you are using timer to invoke any SDK routine, you should put the aw_wait(0) into the timer itself, so you will not interrupt the SDK itself. Hope this helps, -- Andras "It's MY computer" (tm Steve Gibson) r i c h a r dApr 1, 2005, 8:50pm
Its constantly writing the the line "Saying hello..." but never anything
after that, it enters the sub over and over. scifairApr 2, 2005, 12:24am
Good to know. Did you look at the threads window in the debugger? Is it
creating a new one each time it does this, or is it re-using the same thread? -- Rich [View Quote] r i c h a r dApr 3, 2005, 10:59am
Partial work around but it seems a bit messy to me?
Imports System.Timers Imports System.Threading Imports AWSDKLib3 Imports AWSDKLib3.AW_SDK_CONSTANTS Module Module1 Dim sdk As AwSdk3 Dim awwaitthread As Thread Sub Main() Dim aTimer As New System.Timers.Timer(100) AddHandler aTimer.Elapsed, AddressOf OnTimedEvent sdk = New AwSdk3 sdk.aw_init(AW_BUILD) sdk.QuickStart('login code here) sdk.aw_state_change() sdk.aw_say("Hi!") aTimer.Enabled = True aTimer.AutoReset = True awwaitthread = New Thread(AddressOf awwait) awwaitthread.Start() End Sub Private Sub OnTimedEvent(ByVal source As Object, ByVal e As ElapsedEventArgs) Console.WriteLine("Saying Hello World!...") sdk.aw_say("Hello World!") Console.WriteLine("Said Hello World!...") End Sub Public Sub Awwait() Do sdk.aw_wait(1) Loop End Sub End Module |