c# - Why does this thread cause my application to hang up after multiple executions? -


I created this test program to try to understand a little better threading:

  Nom place thread test {public class 1 class} {static number number = new number (); Get Stable Public Zero () {Thread T1 = New Thread (New Thread Start (Increase Number)); t1.Start (); IncrementNumber (); } Public stable zero incrementNumber () {number.Inc (); }} Public class number {int x = 0; Public Zero Inc () {x ++; Debug.Write (x); Debug.Write (Environment.NewLine); } Public override string toasting () {return x.ToString (); }}}   

I call it Thread Test Class 1 Go and execute from the immediate window. I saw that from one execution until the next, unless it needs to be compiled again, static members live in memory and it is using them again.

Here's what the immediate window looks like after the four continuous execution:

  Thread Tests. Class 1 Go () 2 1 expression has been evaluated and it has no value Thread Test. Clash 1. Go (3) 4 evaluation has been evaluated and it has no value Thread Tests. Class 1 Get () ThreadTest.Class1.Go () An expression evaluation is already in progress   

The first execution looks like I expect

Second execution Not designed to write the last environment. new line. Is it possibly because the execution from the main thread was exhausted before executing on T1, so that T1 has the time to write the number, but not the new line? It seems impossible, but this is the only explanation I can come up with now.

The third execution is just hanging, which gives evidence from the production of the fourth attempt to execute it. Why would this hang?

In fact, the execution of the main thread could stop at that point.

It is a good indication that if you get different outputs on various run.

You have to describe the "hang" to fully respond to that aspect. If there is no output, then the main thread will end before any output from t1 . If there is no output but the program does not end, try to break the execution and see which thread is on the line for clues.

Try to join t1 in your main thread to stop the program from exiting

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -