c# - Reading and setting property multiple threads -


my project has 3 classes, , 2 threads. when access property on class thread created from, right value. class i'm reading starts second thread. new thread want read property second class.

when set value in class1 value 1, value in class3 0.

class test {     public void main()     {         class2 cl = new class2;         thread th = new thread(new threadstart(a.start));         th.start()          cl.test=1;     } }  class class2 {     private int test;     public int test     {         { return test;}         set {test = value;}     }      public void start()     {         class3 cls = new class3();         thread th = new thread(new threadstart(cls.begin));         th.start();     } }  class class3 {     public void begin()     {         class2 cl = new class2();         messagebox.show(cl.test.tostring());     } } 

you've got 2 separate instances of class2. instance created in class3 has no idea values in instance created in class1.

if know want single instance of test property deal with, make static:

public static int test { get; set; } 

and reference using:

class2.test = 1; 

as aside, i'm not sure how compiles since you've got public property named "test" access private "test" variable in class2. typically, people name private variable _test (depends on personal preference), or omit private variable altogether did above if property doing nothing other accessing private variable.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -