"BackgroundWorker" in BlitzMax
BlitzMax Forums/MaxGUI Module/"BackgroundWorker" in BlitzMax
| ||
Hello, in Visual Studio there's BackgroundWorker class for time consuming tasks. How this should be done in BlitzMax? I would like to have stop button that can be used to stop the time consuming task. |
| ||
There was a worker-like implementation in the threading module if I'm right |
| ||
Could someone help me out. The following seems to be wrong way of doing it:thread:TThread = CreateThread (Test, Null) While ThreadRunning (thread) If KeyHit(KEY_ESCAPE) Then DetachThread(thread) Delay 1 Wend End Function Test:Object (data:Object) Repeat i = i + 1 Print i Until i = 1000000 End Function (remember to check "Threaded Build" from the IDE) |
| ||
Hey, The correct way to do what you want is to have a thread messaging system to allow threads to communicate. In the worker thread you'd poll for messages while doing the heavy work. In the main app you could then send a message to the thread to stop what it is doing and exit. Don't ever try to 'kill' threads, always let the exit correctly. Now that would the 'correct way' of doing things ;-) On the other hand you could throw together something that uses an atomic variable and switch it using the correct atomic functions, but this isn't scalable and would only be any good for its single purpose. I don't endorse this kind of multithread code but hey... if nothing else, it serves as a learning aid for you :^) Strict Global ExitThreadNow:Int = False Local thread:TThread = CreateThread (Test, Null) ' switch on polledinput for keypresses - otherwise key presses go to the console in the IDE ' might not need this in a real background app. Graphics 200,200 ' DEMO PURPOSES ONLY While ThreadRunning (thread) If KeyHit(KEY_ESCAPE) Then AtomicSwap(ExitThreadNow,True) Delay 1 Wend Print "App exiting" End Function Test:Object (data:Object) Local CarryOnWorking:Int = True Repeat Print "Working...." If CompareAndSwap(CarryOnWorking,ExitThreadNow,False) Exit EndIf Forever Print "Thread exited" End Function |
| ||
Thanks! I haven't done much in BlitzMax yet. This was my first try to use threads. |
| ||
About the stop button... In Visual Studio button's appearence changes, if the button is disabled. In BlitzMax (MaxGUI) disabled button (TGadget) looks the same as enabled button. What's the usual way to handle this? Should I install some other module for this purpose? Which? |
| ||
Strict Import maxgui.drivers Local win:TGadget = CreateWindow("Test",0,0,300,300) Local button:TGadget = CreateButton("STOP",20,20,120,20,win) DisableGadget(button) Repeat WaitEvent Local ev:TEvent = CurrentEvent Select ev.id Case EVENT_WINDOWCLOSE Exit EndSelect Forever Yes DisableGadget and EnableGadget are the correct functions. This code shows a disabled button gadget for me. It changes the visual look ( fades the border and text ) to show that it's disabled. As a side note, I notice that you didn't use Strict or SuperStrict in your code. I've not sure if you are in your real code ( as opposed to not when posting snippets to the forum ), but my advise is to always use at least Strict. It will help catch any typos in your code that, without the use of some kind of 'strictness', will still compile and run but could contain bugs. |
| ||
I must have been blind, because I didn't notice this myself. Perhaps I should use my glasses more often... :-) |