Results 1 to 14 of 14

Thread: [RESOLVED] Threading Problems

  1. #1

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Resolved [RESOLVED] Threading Problems

    Hello
    I have a little problem with threads that it's driving me crazy...
    I have this application, that when it starts it needs to check some files in hard drive, check some info from the database, and other things where the user doesn't need to interact, i created some threads in the constructor of the main form to handle this job, but i don't understand why, when i try to run some of the threads (didn't figured out if all or some specific), the code of the constructor runs from the start again...

    There's a problem when trying to run multiple threads? I already tried the background worker, the thread class and the result it's always the same...

    By the way, i have a splash screen that runs in another thread to update the messages.

    The code it's similar to this:
    vb.net Code:
    1. Public Sub New()
    2.    
    3.         InitializeSplashScreen()
    4.         SplashS.UpdateMessage("First Task...")
    5. ...
    6. other tasks in the same thread
    7. ...
    8.         SplashS.UpdateMessage("First thread...")
    9.         ThreadUpdateModels()
    10. ...
    11.    
    12.  
    13. ...
    14. End Sub


    vb.net Code:
    1. Private Sub ThreadUpdateModels()
    2.         Dim thr As New Thread(AddressOf GeralPDM.GetLatestVersion)
    3.         With thr
    4.             .IsBackground = True
    5.             .Start()
    6.         End With
    7.     End Sub

    Some of the functions used by the threads use other functions than the one called directly from the class, is this the problem?

    Can i inside a thread call a another thread, i have a situation that i need to check if a connection it's on, before getting the data, so i use another thread?

    Thanks

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Threading Problems

    Code:
    Public Sub New()
        
            InitializeSplashScreen()
            SplashS.UpdateMessage("First Task...")
    ...
    Please tell me that the New Sub is not from a form called SplashS? What's the name of the class with this code in it?
    Last edited by Grimfort; Jan 18th, 2012 at 02:13 PM.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Threading Problems

    You seem to have a bit of confusion about threads. A thread is a series of function calls and code. Once you start that thread, any code executed as part of the thread is....well....part of the thread. Multiple threads can access objects that are in scope for all of them, but the functions don't matter. One thread can certainly start another thread, or else you wouldn't be able to have threading, since the UI thread is also a thread. You can raise events on other threads, but that's different.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Threading Problems

    @Grimfort
    No it's not...
    It works fine if i don't try to start another threads from the main form...

    @Shaggy Hiker
    Yes i know that, but like i wrote, just for adding some threads to the constructor the code at certain point makes the code of the constructor to run again... and after this situation i just needed to be sure that my assumptions weren't wrong...

    The flow of the code:
    Constructor Main Form
    ---Splash Screen Different Thread to be able to update the UI
    ---Update Splash
    ---Call of First Thread
    -------First Thread calls other function on module
    ---Update Splash
    ---Call of Second Thread
    -------Second Thread calls other thread to check state of a connection
    -------IF connection ON then calls another Sub
    ---Update Splash
    ---Call of Third Thread
    ------Third Thread check for database state
    ----------If state isn't updated call sub to update
    End Constructor

    Looks like that i have to debug each one of the threads individually...
    Last edited by mickey_pt; Jan 18th, 2012 at 06:09 PM. Reason: Tabs didn't work

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Threading Problems

    It sure sounds like you are creating a new instance of that main form. You wouldn't happen to be using default instances would you?
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Threading Problems

    No i'm not...
    Default instances?!

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Threading Problems

    What does that mean? Are you not sure what default instances are? A default instance is where you don't create a specific instance of a form:

    Dim nf As New Form1
    nf.Whatever 'Uses a specific instance of Form1

    but just use

    Form1.Whatever 'Uses the default instance of Form1.

    The reason I ask is that it really would be possible for a default instance constructor to behave in an odd way. They are kind of arcane to begin with, and cause plenty of problems.
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Threading Problems

    No i don't use, at least in this section of the code.
    This is the main form and i don't make any reference to it, because it's the default form for the app to start, and the only form that i have at this point it's the splash, the rest of the code doesn't have any gui.

    Tomorrow, I'll comment all the threads, and I'll test one by one, to see or try to see, where the code jumps to the start of the constructor...

    Thanks

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  9. #9
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Threading Problems

    You do not have to do anything clever to debug multiple threads. You say that the Sub New is running from the start, which is only possible if you are actually creating another one of these objects. If you stick a breakpoint on the first line of the sub and run it, the second time you hit this breakpoint have a look at the call stack to see why. There is also a threads window to show you the other threads which are all paused when the breakpoint is hit.

  10. #10

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Threading Problems

    I know how to debug, the problem of the debuging some of the threads it's that one of them uses a recursive function, other it's a loop that reads more than 50k rows of the database....

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  11. #11
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Threading Problems

    Why would any of that matter? You can see what is calling what with the call stack and the threading window to work out why your constructor is being called multiple times.

  12. #12

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Threading Problems

    Found out the problem...
    Deep in the recursive function i needed the handle of the main form, and forgot to remove the direct reference/default instance...

    Thanks

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  13. #13
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [RESOLVED] Threading Problems

    Ha! The default instance rears its ugly head, once again. I wish those things had never been added.
    My usual boring signature: Nothing

  14. #14
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: [RESOLVED] Threading Problems

    Me hates it precious!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width