c# - Threads creation -
please have @ following code. getting head around multithreading in c#. example taken manual. in main function try create 3 threads.
could please tell me how 2 threads (with errors) should created?
thank you!
using system; using system.threading; public static class monitormethodaccess { private static int numericfield = 1; private static object syncobj = new object(); public static object syncroot { { return syncobj; } } public static void incrementnumericfield() { if (monitor.tryenter(syncobj, 250)) { try { ++numericfield; } { monitor.exit(syncobj); } } } public static void modifynumericfield(int newvalue) { if (monitor.tryenter(syncobj, 250)) { try { numericfield = newvalue; } { monitor.exit(syncobj); } } } public static int readnumericfield() { if (monitor.tryenter(syncobj, 250)) { try { return (numericfield); } { monitor.exit(syncobj); } } return (-1); }
}
class program { static void main(string[] args) { thread aaa = new thread(monitormethodaccess.incrementnumericfield); thread bbb = new thread(monitormethodaccess.modifynumericfield(12);//error thread ccc = new thread(monitormethodaccess.readnumericfield);//error console.readkey(); } }
thread bbb = new thread(()=>monitormethodaccess.modifynumericfield(12)); thread ccc = new thread(() => monitormethodaccess.readnumericfield());
but if want return values threads in third thread, should think use tasks
Comments
Post a Comment