Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Problem with Thread.Start

Problem with Thread.Start

Scheduled Pinned Locked Moved .NET (Core and Framework)
helpcareer
10 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bob_Sun
    wrote on last edited by
    #1

    I have a problem with Thread.Start. In my program, serveral threads are started asyncrously, ie. thread1.Start(); thread2.Start(); ... threadn.Start(); All these threads are started by a thread manager and exit after they have finished their jobs. But, just once, thread2 to threadn were all started and finished normally, while thread1 didn't continue its job. Fortunately, the thread manager could Abort thread1 by its handler. Up to now, this happend only once, and it was not able to log the thread status when abort it. Could anyone give some advice about why the thread will hang up even though it is started (I could confirm it by log) Thank you

    G 1 Reply Last reply
    0
    • B Bob_Sun

      I have a problem with Thread.Start. In my program, serveral threads are started asyncrously, ie. thread1.Start(); thread2.Start(); ... threadn.Start(); All these threads are started by a thread manager and exit after they have finished their jobs. But, just once, thread2 to threadn were all started and finished normally, while thread1 didn't continue its job. Fortunately, the thread manager could Abort thread1 by its handler. Up to now, this happend only once, and it was not able to log the thread status when abort it. Could anyone give some advice about why the thread will hang up even though it is started (I could confirm it by log) Thank you

      G Offline
      G Offline
      George L Jackson
      wrote on last edited by
      #2

      There are too many reasons to list since you have not provided any code, pseudo-code or even a brief discription of what your threads are doing. Are you using try-catch-finally blocks or assertions in your thread code? Are your threads modifying the same data?

      B 1 Reply Last reply
      0
      • G George L Jackson

        There are too many reasons to list since you have not provided any code, pseudo-code or even a brief discription of what your threads are doing. Are you using try-catch-finally blocks or assertions in your thread code? Are your threads modifying the same data?

        B Offline
        B Offline
        Bob_Sun
        wrote on last edited by
        #3

        George L. Jackson wrote:

        There are too many reasons to list since you have not provided any code, pseudo-code or even a brief discription of what your threads are doing. Are you using try-catch-finally blocks or assertions in your thread code? Are your threads modifying the same data?

        Thank you for your reply. As it is impossible to attach too many source code, I hope a brief discription will help. I use a thread manager of my own to create threads, keep track of the handlers of them while the threads are working and delete each handler just before each thread exits. All the threads execute in background and have a priority "normal". Threads of the same type will have the same thread name. Before the object which owns the thread manager dispose, it will also call the Dispose method of thread manager in which all thread handlers will be checked and aborted if the ThreadState is not "Stopped" (if a thread exits normally, no reference of finished thread will remain in thread manager 's list). Of cause, if any of the threads stopped abormally, the object which owns thread manager will hang up unless it is stopped by some external "event" . From the help of MSDN, it is said a thread will be set to Running state whenever Thread.Start() is executed and will be scheduled by OS. I have never imagined a Started thread will stop somethere. You see, I don't use Thread directly but instead use MyThread, and all the methods have try-catch for exceptions. As for the stopped thread, if it executes , a message must be logged first which never appeared when that thread hanged. All the threads will entered the same statemachine and lock is used with a 10 seconds timer. As no timeout occured at all, no deadlock occured. Could you give me some hints about "the too many reasons" please ? Thank you

        G 1 Reply Last reply
        0
        • B Bob_Sun

          George L. Jackson wrote:

          There are too many reasons to list since you have not provided any code, pseudo-code or even a brief discription of what your threads are doing. Are you using try-catch-finally blocks or assertions in your thread code? Are your threads modifying the same data?

          Thank you for your reply. As it is impossible to attach too many source code, I hope a brief discription will help. I use a thread manager of my own to create threads, keep track of the handlers of them while the threads are working and delete each handler just before each thread exits. All the threads execute in background and have a priority "normal". Threads of the same type will have the same thread name. Before the object which owns the thread manager dispose, it will also call the Dispose method of thread manager in which all thread handlers will be checked and aborted if the ThreadState is not "Stopped" (if a thread exits normally, no reference of finished thread will remain in thread manager 's list). Of cause, if any of the threads stopped abormally, the object which owns thread manager will hang up unless it is stopped by some external "event" . From the help of MSDN, it is said a thread will be set to Running state whenever Thread.Start() is executed and will be scheduled by OS. I have never imagined a Started thread will stop somethere. You see, I don't use Thread directly but instead use MyThread, and all the methods have try-catch for exceptions. As for the stopped thread, if it executes , a message must be logged first which never appeared when that thread hanged. All the threads will entered the same statemachine and lock is used with a 10 seconds timer. As no timeout occured at all, no deadlock occured. Could you give me some hints about "the too many reasons" please ? Thank you

          G Offline
          G Offline
          George L Jackson
          wrote on last edited by
          #4

          So, what does thread execute before it stores a log message? Also, how are you logging your messages?

          B 1 Reply Last reply
          0
          • G George L Jackson

            So, what does thread execute before it stores a log message? Also, how are you logging your messages?

            B Offline
            B Offline
            Bob_Sun
            wrote on last edited by
            #5

            George L. Jackson wrote:

            So, what does thread execute before it stores a log message?

            Nothing else.The first line of the thread is to store a message.

            George L. Jackson wrote:

            Also, how are you logging your messages?

            A log host service application exists in my system. All the threads in my main program (a service of XP) send log messages to a log client inside the main program(first into a quue). The log client send to log host by remoting FIFO. Both the client and the host are working after a certain thread hanged up. Thank you

            G 2 Replies Last reply
            0
            • B Bob_Sun

              George L. Jackson wrote:

              So, what does thread execute before it stores a log message?

              Nothing else.The first line of the thread is to store a message.

              George L. Jackson wrote:

              Also, how are you logging your messages?

              A log host service application exists in my system. All the threads in my main program (a service of XP) send log messages to a log client inside the main program(first into a quue). The log client send to log host by remoting FIFO. Both the client and the host are working after a certain thread hanged up. Thank you

              G Offline
              G Offline
              George L Jackson
              wrote on last edited by
              #6

              Does each thread access the queue directly? Are you using a synchronized queue? Also, you probably know this already, threads don't always start executing in the same order that you executed Thread.Start.

              B 1 Reply Last reply
              0
              • B Bob_Sun

                George L. Jackson wrote:

                So, what does thread execute before it stores a log message?

                Nothing else.The first line of the thread is to store a message.

                George L. Jackson wrote:

                Also, how are you logging your messages?

                A log host service application exists in my system. All the threads in my main program (a service of XP) send log messages to a log client inside the main program(first into a quue). The log client send to log host by remoting FIFO. Both the client and the host are working after a certain thread hanged up. Thank you

                G Offline
                G Offline
                George L Jackson
                wrote on last edited by
                #7

                This might be helpful: http://www.anticipatingminds.com/Content/Products/devAdvantage/KnowledgePacks/Threading/ThreadingKnowledgePack.aspx[^]

                1 Reply Last reply
                0
                • G George L Jackson

                  Does each thread access the queue directly? Are you using a synchronized queue? Also, you probably know this already, threads don't always start executing in the same order that you executed Thread.Start.

                  B Offline
                  B Offline
                  Bob_Sun
                  wrote on last edited by
                  #8

                  George L. Jackson wrote:

                  Does each thread access the queue directly?

                  Yes, each thread access the thread directly but synchronized.

                  George L. Jackson wrote:

                  Also, you probably know this already, threads don't always start executing in the same order that you executed Thread.Start.

                  As you have pointed out, I have taken this point into condsideration. But, the fact is although my program rarely hangs up, it happened when some factors occured at the same time.

                  G B 2 Replies Last reply
                  0
                  • B Bob_Sun

                    George L. Jackson wrote:

                    Does each thread access the queue directly?

                    Yes, each thread access the thread directly but synchronized.

                    George L. Jackson wrote:

                    Also, you probably know this already, threads don't always start executing in the same order that you executed Thread.Start.

                    As you have pointed out, I have taken this point into condsideration. But, the fact is although my program rarely hangs up, it happened when some factors occured at the same time.

                    G Offline
                    G Offline
                    George L Jackson
                    wrote on last edited by
                    #9

                    Are you synchronizing per this article: http://www.informit.com/guides/content.asp?g=dotnet&seqNum=143[^]

                    1 Reply Last reply
                    0
                    • B Bob_Sun

                      George L. Jackson wrote:

                      Does each thread access the queue directly?

                      Yes, each thread access the thread directly but synchronized.

                      George L. Jackson wrote:

                      Also, you probably know this already, threads don't always start executing in the same order that you executed Thread.Start.

                      As you have pointed out, I have taken this point into condsideration. But, the fact is although my program rarely hangs up, it happened when some factors occured at the same time.

                      B Offline
                      B Offline
                      Bob_Sun
                      wrote on last edited by
                      #10

                      I would like to continue this thread because it is not solved yet. I realize these days that there may be some reasons in the total threads in my application. In a app., what should the total number of threads be? Here threads include all thread invoked by mechanisim such BeginInove and Thread.Start(). In my appl., the active threads number ranges from 40 to more than 60. I don't think this is proper, but threads number under my control is just no more than 5. Who should be responsible for all these threads? Many remoting process are undergone in my application. Thank you!

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups