json - Strange behavior doing multithreaded activity in C# -


i've got simple program enter , serialize object using lambda expression pass things off thread.

using system; using system.threading;  using newtonsoft.json;  namespace multithreadingapplication {     class threadcreationprogram     {         static void main(string[] args)         {             myobject theobject = new myobject();             console.writeline("enter following:");             console.writeline("color:");             theobject.color = console.readline();             console.writeline("number");             theobject.color = console.readline();             console.writeline("shape:");             theobject.shape = console.readline();              thread mynewthread = new thread(() => serialize(theobject));             mynewthread.start();                         mynewthread.abort();                         console.readkey();         }          public static void serialize(myobject theobject)         {             string json = jsonconvert.serializeobject(theobject, formatting.indented);             console.writeline(json);             thread.sleep(1000);         }     }      public class myobject     {         private int32 number;         private string color, shape;          public int32 number         {             { return number; }             set { number = value; }         }          public string color         {             { return color; }             set { color = value; }         }          public string shape         {             { return shape; }             set { shape = value; }         }          public myobject()         {          }     } } 

when run thing, notice sometimes, won't call serialize method. i've examined breakpoints , instantiate thread lambda expression statement , terminate without ever going down serialize method. i'm new multithreading, what's deal here?

mynewthread.start();             mynewthread.abort(); 

the thread fails make progress because abort before has chance execute. if want thread execute don't abort it.

the whole point of threads execute independent of each other. when call start thread instructed begin executing. in meantime calling thread free continue. in case aborts thread. , can happen before thread has got going.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -