1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Allow one instance of form while threading

Discussion in 'Visual Basic .NET' started by captchaman, Aug 24, 2011.

  1. captchaman

    captchaman Junior Member

    Joined:
    Sep 16, 2010
    Messages:
    190
    Likes Received:
    842
    Occupation:
    Software Programmer
    Location:
    USA
    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: Aug 24, 2011
  2. ShiftySituation

    ShiftySituation Power Member

    Joined:
    Apr 15, 2010
    Messages:
    621
    Likes Received:
    314
    Occupation:
    Having fun
    Location:
    Jacksonville, FL
    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.
     
    • Thanks Thanks x 1
  3. ShiftySituation

    ShiftySituation Power Member

    Joined:
    Apr 15, 2010
    Messages:
    621
    Likes Received:
    314
    Occupation:
    Having fun
    Location:
    Jacksonville, FL
    • Thanks Thanks x 1
  4. captchaman

    captchaman Junior Member

    Joined:
    Sep 16, 2010
    Messages:
    190
    Likes Received:
    842
    Occupation:
    Software Programmer
    Location:
    USA
    Tried SynLock, it didn't help. Really don't know what to think of this.
     
  5. smack

    smack Junior Member

    Joined:
    Feb 1, 2010
    Messages:
    182
    Likes Received:
    78
    Occupation:
    Software Engineer/Evil Genius
    Location:
    inside .NET
    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.
     
  6. ShiftySituation

    ShiftySituation Power Member

    Joined:
    Apr 15, 2010
    Messages:
    621
    Likes Received:
    314
    Occupation:
    Having fun
    Location:
    Jacksonville, FL
    SyncLock should work... how did you go about using it?
     
  7. scriptomania

    scriptomania Junior Member

    Joined:
    Dec 28, 2010
    Messages:
    127
    Likes Received:
    249
    Occupation:
    A full time pirate at sea
    Location:
    The European capital of politics
    No clue about VB, but generally speaking, you need to look at thread isolation objects. Usually mutex.

    Hope this helps.
     
  8. Hydrogen

    Hydrogen Newbie

    Joined:
    Dec 30, 2009
    Messages:
    39
    Likes Received:
    23
    Occupation:
    Co-Owner of AdvertMarketing
    Home Page:
    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: Sep 12, 2011