Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DoEvents and Modal Forms = Hell

Status
Not open for further replies.

RaKKeR

Programmer
Nov 22, 2004
26
0
0
NL
Dear Programmers,

I'm experiencing a problem that is a real pain in the ass. As VB doesn't support threading, our project uses a VC++ dll that starts some thread for heavy load processes. This is how we wait for the thread to finish:

...
While WaitForSingleObject(ThreadID,200) = WAIT_TIMEOUT
DoEvents 'don't block the user interface
Wend
...

The problem is that when the DoEvents method gets called when the user clicked a button to open a modal form, the DoEvents method never returns till the user closes the form again (this is understandable, coz DoEvents tries to execute all pending window messages and the modal form waits for the user to close it).

Is there any other way to wait for the thread to end without blocking the user interface (getting rid of the doevents would cause this)?

Thx in advance,

RaKKeR
 
That's why I previously said there wasn't an easy solution. I'm sorry that no-one here can help you reinvent the way single-threaded VB works by providing the wished for two or three lines of trivial code that have no impact on your design, but there you go.
 
Don't feel sorry, I just hoped someone else experienced the same situation where doing a redesign would take more than a month or so, and who found a workaround for it... Is there any change I could suspend the current event handler, give some time to the main GUI thread, and resume the event handler afterwards? Even if that would take a week to implement, it would be better than having to rewrite code that has evolved in several years and would take months to redesign... I was thinking of the same way interrupts are handled, wouldn't that be possible in the same thread?
 
Hi,

I'll try to workaround the problem by making it possible for users to interrupt the thread with mouseclicks (e.g. when trying to open modal form). Can someone tell me if I'm missing something in this code snippet?

Code:
'**********************************************************************************************************
' WaitForThread: waits for thread while processing window messages, but can be interrupted by click events*
'**********************************************************************************************************
Public Function WaitForThread(ThreadID As Long) As Boolean
    Dim udtMsg As Msg
    Dim interrupted As Boolean
    Dim messagesAvailable As Boolean
    
    interrupted = False
    While Not interrupted And WaitForSingleObject(ThreadID, 100) = WAIT_TIMEOUT
        interrupted = False
        messagesAvailable = True
        While Not interrupted And messagesAvailable
            If PeekMessage(udtMsg, 0&, 0, 0, PM_NOREMOVE) > 0 Then 'if message available
                If udtMsg.message <> WM_LBUTTONUP Then 'if user did not click button
                    If PeekMessage(udtMsg, 0&, 0, 0, PM_REMOVE) Then 'handle the message
                        TranslateMessage udtMsg
                        DispatchMessage udtMsg
                    End If
                Else
                    interrupted = True
                End If
            Else
                messagesAvailable = False
            End If
        Wend
    Wend
    WaitForThread = Not interrupted
End Function

Thx in advance,

RaKKeR
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top