Allow one instance of form while threading

captchaman

Junior Member
Joined
Sep 16, 2010
Messages
192
Reaction score
848
I'm making a multithreaded account creator and I want the user to be able to choose between using DeathByCaptcha and manually entering CAPTCHAs. However, I only want frmCAPTCHA to show one instance at a time so that the user doesn't get bombarded with windows while trying to type in one captcha. In the sub that I multithread, I have this code:
Code:
        If chkUseDBC.Checked = True Then
            'DBC code
        ElseIf chkUseDBC.Checked = False Then
            Do While frmCAPTCHA.isLoaded = True
                Delay(0.1)
            Loop
            frmCAPTCHA.Filernd = Filernd
            frmCAPTCHA.Show()
            Do While TheCAPTCHA = ""
                Delay(0.1)
            Loop
        End If
In frmCAPTCHA, I have registered
Code:
Public isLoaded as Boolean = False
and
Code:
isLoaded = True
on Form_Load
But, even if I delay QueUserWorkItem one second, frmCAPTCHA still opens multiple instances.

Even if I use the code:
Code:
If frmCAPTCHA.isLoaded = True Then
                MsgBox("Already loaded")
                Exit Sub
            End If
            frmCAPTCHA.Show()
            Exit Sub

it STILL opens the form multiple times AND shows the msgbox.

Is there something I'm missing with threading? I don't see any problems with my code.
 
Last edited:
Not sure about VB, I'm a c# coder but I'm sure there is a thread lock method for VB. You can write a method to show the captcha to the user and write it within a lock method which will only allow one thread to enter that method at a time, in turn only showing one captcha window to the end user at a time.
 
Tried SynLock, it didn't help. Really don't know what to think of this.
 
so what i would recommend is that instead of trying to restrict it to one instance of the form, you dump all your pending captchas to a central queue. the form with the image box loops in the background and then when it sees things in that queue it grabs them out and displays them. you don't actually want to have multiple instances of the captcha form. just one instance accessing a centralized collection that all the threads are dumping images to.

you're still going to need your SyncLock statements on the shared resources, but from the architectural perspective you're looking at it backwards.
 
No clue about VB, but generally speaking, you need to look at thread isolation objects. Usually mutex.

Hope this helps.
 
If this is the form you are talking about, then instead of

Code:
frmCAPTCHA.Show()

use

Code:
frmCAPTCHA.ShowDialog()
instead.

ShowDialog works the same as Show (however it does not allow you to access anything else but that form until it's closed. Think of it as a msgbox but instead of a message it's your whole form.

If that doesn't work then you can try this....

Code:
Dim keepRunning as Boolean
...
While keepRunning 'keeps thread running forever
     SyncLock accessLock
           If endThread Then
                keepRunning = False
           End If
     End SyncLock
End While
 
Last edited:
Back
Top