Peculiar bug or just me ?

BlitzPlus Forums/BlitzPlus Programming/Peculiar bug or just me ?

Oso(Posted 2004) [#1]
Without going into unnecessary detail, I am developing a reasonably involved GUI application. I noticed a very peculiar difference in behaviour from one computer to another. The original code is large but I have managed to write this little one which shows the issue clearly with dummy calculations.

Basically, when the user takes the "Start loop" option, a long calculation is set in motion which the user may interrupt with ESC. A Flushkeys is executed upon entry to the function in case more than one ESC or anything else has been keyed previously.

The funny thing is that it works perfectly on my computer at home, but on my computer at work Flushkeys doesn't appear to clear anything. i.e. if 3 ESCs have been pressed, 3 clicks on "Start loop" become necessary to perform the function without exiting. If x ESCs are pressed, x clicks on "Start loop" are needed.

So for the same code, one computer executes Flushkeys correctly and one doesn't. In every other way their function is identical.

What have I missed here ? I cannot imagine why this should be so.

;Make a simple window
window=CreateWindow("",50,50,200,200,0,7)
menu=WindowMenu(window)
menu1=CreateMenu("Test",1,menu)
menu1_1=CreateMenu("Start loop",11,menu1)
UpdateWindowMenu window
Global workarea=CreateCanvas(0,0,200,200,window)
SetBuffer CanvasBuffer(workarea)
ClsColor 0,0,0
Cls
Color 255,255,255

;Event loop - only two will do for the purpose of this example
While WaitEvent()
Select EventID()
Case $803
End
Case $1001
If EventData()=11 Then doloop()
End Select
Wend

;Slow operation involving lots of calculation
;but interruptible by ESC
Function doloop()
FlushKeys
For i=0 To 100000
;Show that the operation is actually doing something
If (i Mod 1000)=0 Then
Cls
Text 20,20,Str(i)
FlipCanvas workarea
EndIf
For j=0 To 100000
k=i*j
Next
If GetKey()=27 Then Exit
Next
End Function


Mr Brine(Posted 2004) [#2]
hi!

Had a look at your code seems like a pretty weird bug, though I did seem to have a bit of luck. But you'll have to try it out and let us know if it resolved yer probs.

replace the doloop function with this:

Function doloop()
For i=0 To 100000
;Show that the operation is actually doing something
If (i Mod 1000)=0 Then
Cls
Text 20,20,Str(i)
FlipCanvas workarea
EndIf
For j=0 To 100000
k=i*j
Next
If KeyDown(1) Exit
Next
End Function

Well good luck!!

Mr Brine


Oso(Posted 2004) [#3]
Mr Brine:

Thank you. Keydown and Keyhit work but Getkey/Flushkeys do not. The trouble with fixing something without knowing what is going on underneath, precisely what each command does, is that you can never really be sure in advance that something you write will work.

I do have a personal programming weakness with events, menus, keypresses, mice and so on. It was the same on the Amiga. I have no trouble with extremely complicated algorithms, but it's taking me a month of Sundays to really get a cast iron grip on a general structure for user interfaces.

Anyway, thanks again for your trouble.


EOF(Posted 2004) [#4]
Have you tried using the keystoke event instead?
Should prove more system-friendly.
See if this helps ...

;Make a simple window 
window=CreateWindow("",50,50,200,200,0,7) 
menu=WindowMenu(window) 
menu1=CreateMenu("Test",1,menu) 
menu1_1=CreateMenu("Start loop",11,menu1) 
UpdateWindowMenu window 
Global workarea=CreateCanvas(0,0,200,200,window) 
SetBuffer CanvasBuffer(workarea) 
ClsColor 0,0,0 
Cls 
Color 255,255,255 

;Event loop - only two will do for the purpose of this example 
While WaitEvent() 
	Select EventID() 
		Case $803
			End 
		Case $1001 
			If EventData()=11 Then doloop() 
	End Select 
Wend 

;Slow operation involving lots of calculation 
;but interruptible by ESC 
Function doloop() 
	FlushEvents
	For i=0 To 100000 
		;Show that the operation is actually doing something 
		If (i Mod 100)=0
			Cls 
			Text 20,20,Str(i)
			FlipCanvas workarea 
		EndIf 
		For j=0 To 100000 
			k=i*j
		Next 
		If WaitEvent(1)=$103 ; keystroke
			If EventData()=27 ; [ESC] key
				FlushEvents
				Cls
				Text 20,20,"Interrupted"
				FlipCanvas workarea 
				Exit
			EndIf
		EndIf
	Next 
End Function



Oso(Posted 2004) [#5]
No, actually I hadn't tried that one - probably better because it is more uniform with windows events. They all work on my computer here at home though. I'll try it at work tomorrow.

Thank you.


Oso(Posted 2004) [#6]
Okay, the method of using windows key events seems to work everywhere. It leaves open the question of exactly when to use Getkey, Keydown, Hitkey, Flushkeys etc, and what exactly they do, but I can leave that for the purpose of the present exercise.

Thanks to all who answered.