Problem with Thread.Start
-
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 -
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 youThere 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?
-
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?
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
-
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
So, what does thread execute before it stores a log message? Also, how are you logging your messages?
-
So, what does thread execute before it stores a log message? Also, how are you logging your messages?
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
-
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
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.
-
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
-
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.
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.
-
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.
Are you synchronizing per this article: http://www.informit.com/guides/content.asp?g=dotnet&seqNum=143[^]
-
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.
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!