SOLVED: Is simulated multithreading the way to go (for what i need to do)?
First off, let me say I'm an experienced programmer, but I'm still reorienting myself with VB which I hadn't touched in some time.
I'm writing an app with a heavy-duty function call that takes about 5 minutes to complete (processing a list of files inside a loop). What happens is, the user clicks "Start" which triggers said function, and the program (predictably) becomes unresponsive until the function completes. What i'd like, however, is for the user to still be able to move the window or at least click a Cancel button if they need to abort it (the function itself is primarily a loop, so I could add a check at the end of said loop).
Now, I've read the horror stories about simulated multithreading in vb6, so I'm wondering...is there another (better) way?
Re: Is simulated multithreading the way to go (for what i need to do)?
If you are just doing something in a loop then it may not be a candidate for multithreading.
Do you have a doevents in the loop and a boolean variable flag to signify the breaking out of the loop?
Re: Is simulated multithreading the way to go (for what i need to do)?
DoEvents is what I was looking for :o
As I said, still accustoming myself to vb, so chock this one up as a stupid question
Re: SOLVED: Is simulated multithreading the way to go (for what i need to do)?
There are no stupid questions. :)
Ask any questions and we will try to help (just create a new thread for anything new ;))
Glad to have helped.
Re: SOLVED: Is simulated multithreading the way to go (for what i need to do)?
You would need to put DoEvents which should execute at regular intervals in your application. However as the normal DoEvents is very slow, just have a look at this link which provides an alternative faster DoEvents function.
Give Time to Other Events in My Application
Pradeep :)
Re: SOLVED: Is simulated multithreading the way to go (for what i need to do)?
You can speed it up pretty well by using DoEvents only when it is needed. You can use the GetInputState API function for this.
VB Code:
Private Declare Function GetInputState Lib "user32" Alias "GetInputState" () As Long
Do
DoSomething
If GetInputState Then DoEvents
Loop Until Condition = True
There is another (better) API function to use, but I forgot what it is. Hopefully someone remembers and will post it.
Re: SOLVED: Is simulated multithreading the way to go (for what i need to do)?
That is a great API DR but what about mouse moves over the GUI? Wouldnt it not process those and give the locked illusion? Seems the api is only for mouse clicks and keyboard keypresses.
Good tip though :thumb:
Re: SOLVED: Is simulated multithreading the way to go (for what i need to do)?
Quote:
Originally Posted by RobDog888
That is a great API DR but what about mouse moves over the GUI? Wouldnt it not process those and give the locked illusion? Seems the api is only for mouse clicks and keyboard keypresses.
Good tip though :thumb:
It seems to work for me.
You may have to put a DoEvents right before the loop, not sure.
Re: SOLVED: Is simulated multithreading the way to go (for what i need to do)?
Havent tested it but mostly referring to your gui if you have events that should fire like mouseovers which dont count towards the queue that the api looks at. ;)
Re: SOLVED: Is simulated multithreading the way to go (for what i need to do)?
I know it's marked as resolved, but just in case someone is interested in the multithreading option - a little demo program.
As to which is faster, MultiThreading or DoEvents, I'll leave others to argue. The obvious advantage of MultiThreading comes in when you have to perform other tasks within the same app. Make an executable of the code below and run it. Click on "Start MultiThread" and move the form around, scroll the textbox etc. The "AddTextMultiThread" function continues without even a pause. Click on "Stop MultiThread". Try the same with "Start/Stop DoEvents". Spot the difference.
'Original code at:- http://www.free2code.net/plugins/art...read.php?id=94
VB Code:
Option Explicit
'Form level code.
'MultiThread versus DoEvents.
'Add a textbox and 4 command buttons to a form.
Private Declare Function CreateThread Lib "kernel32" (ByVal lpThreadAttributes As Any, _
ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, _
ByVal dwCreationFlags As Long, lpThreadID As Long) As Long
Private Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, _
ByVal dwExitCode As Long) As Long
Private Sub Command1_Click()
id = CreateThread(ByVal 0&, ByVal 0&, AddressOf AddTextMultiThread, ByVal 0&, 0, id)
End Sub
Private Sub Command2_Click()
Call TerminateThread(id, ByVal 0&)
End Sub
Private Sub Command3_Click()
StopDo = True
AddTextDoEvents
End Sub
Private Sub Command4_Click()
StopDo = False
End Sub
Private Sub Form_Load()
Me.Height = 4545
Me.Width = 8925
Text1.Move 45, 45, 8700, 3525
Command1.Move 45, 3690, 1635, 375
Command1.Caption = "Start MultiThread"
Command2.Move 1845, 3690, 1635, 375
Command2.Caption = "Stop MultiThread"
Command3.Move 5310, 3690, 1635, 375
Command3.Caption = "Start DoEvents"
Command4.Move 7110, 3690, 1635, 375
Command4.Caption = "Stop DoEvents"
End Sub
VB Code:
'Module level code.
Option Explicit
Public id As Long
Public StopDo As Boolean
Public Function AddTextMultiThread()
Do
Form1.Text1.SelText = "Adding to Text1 - "
Loop
End Function
Public Function AddTextDoEvents()
StopDo = True
Do While StopDo = True
Form1.Text1.SelText = "Adding to Text1 - "
DoEvents
Loop
End Function
Re: SOLVED: Is simulated multithreading the way to go (for what i need to do)?
The above code will not work correctly.
The CreateThread API is not supported in VB and will quite often crash your app.
The only method I have seen that is close to tru multithreading in the code contained in the Multithreading link in my sig.
Cheers,
Woka
Re: SOLVED: Is simulated multithreading the way to go (for what i need to do)?
Made the form & module with code as supplied, and when compiled (though not in VB) it runs as advertised. Thank you. Very keen bit of code.
Re: SOLVED: Is simulated multithreading the way to go (for what i need to do)?
Thank you for letting us know that you have your answer. You may find it easier next time however by just pulling down the Thread Tools menu and clicking the Mark Thread Resolved button.
Re: SOLVED: Is simulated multithreading the way to go (for what i need to do)?
it seems like people in here know what they're talking about when it comes to multi-threading.
in that case (since this thread is solved), would anyone like to look at my problem involving multi-threading?
http://www.vbforums.com/showthread.php?t=521236