Threading in C#
-
Hello Good morning all, I want threading to implemented in C#-windows applications with proper syncroniation. Like i have one textfile(resource) and two threads are acting upon reading and writing, now i want to provide syncrinization to that text file with some technques(critical section, semaphores, mutual exclusion etc). can some one help me out with a sample or link so that i can implement it.. Thanks in Advance :)
-
Hello Good morning all, I want threading to implemented in C#-windows applications with proper syncroniation. Like i have one textfile(resource) and two threads are acting upon reading and writing, now i want to provide syncrinization to that text file with some technques(critical section, semaphores, mutual exclusion etc). can some one help me out with a sample or link so that i can implement it.. Thanks in Advance :)
-
Hello Good morning all, I want threading to implemented in C#-windows applications with proper syncroniation. Like i have one textfile(resource) and two threads are acting upon reading and writing, now i want to provide syncrinization to that text file with some technques(critical section, semaphores, mutual exclusion etc). can some one help me out with a sample or link so that i can implement it.. Thanks in Advance :)
-
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick Visit the Hindi forum here.
thanks for ur reply...I have tried it with lock(object) but it is not working... Iam sending my code please go though it once and let me know, where i went wrong. private void Form1_Load(object sender, EventArgs e) { try { sw = new StreamWriter("D:\\Threading.txt"); ThreadStart Thrdst = new ThreadStart(Thread1); Thread t = new Thread(Thrdst); t.Start(); ThreadStart Thrdstt = new ThreadStart(Thread2); Thread tt = new Thread(Thrdstt); tt.Start(); ThreadStart Thrdsttt = new ThreadStart(Thread3); Thread ttt = new Thread(Thrdsttt); ttt.Start(); } catch (Exception err) { label1.Text = err.Message.ToString(); } //Spawnning 3 Threads on form load.. private void Thread1() { Thread1 t1 = new Thread1(); t1.ThreadWriter(sw); } private void Thread2() { Thread2 t2 = new Thread2(); t2.ThreadWriter(sw); } private void Thread3() { Thread3 t3 = new Thread3(); t3.ThreadWriter(sw); } ------------------------------------------------------------------------------------------------------------------------- And i have three diferent clasees which write to text file... class Thread1 { private Object thisLock; public void ThreadWriter(StreamWriter sw) { thisLock = new Object(); lock (this) { for (int i = 0; i < 10; i++) { sw.WriteLine("Thread 1 = " + i); Thread.Sleep(100); } sw.Close(); } } } The other two are almost similar... Output is:- -------------- Thread 1 = 0 Thread 2 = 0 Thread 3 = 0 Thread 1 = 1 Thread 3 = 1 Thread 2 = 1 Thread 1 = 2 Thread 3 = 2 Thread 2 = 2 Thread 1 = 3 Thread 3 = 3 Thread 2 = 3 Thread 1 = 4 Thread 3 = 4 Thread 2 = 4 Thread 1 = 5 Thread 2 = 5 Thread 3 = 5 Thread 1 = 6 Thread 3 = 6 Thread 2 = 6 Thread 1 = 7 Thread 3 = 7 Thread 2 = 7 Thread 1 = 8 Thread 3 = 8 Thread 2 = 8 Thread 1 = 9 Thread 3 = 9 Thread 2 = 9 Please help me out.. Thanks in Advance..
-
thanks for ur reply...I have tried it with lock(object) but it is not working... Iam sending my code please go though it once and let me know, where i went wrong. private void Form1_Load(object sender, EventArgs e) { try { sw = new StreamWriter("D:\\Threading.txt"); ThreadStart Thrdst = new ThreadStart(Thread1); Thread t = new Thread(Thrdst); t.Start(); ThreadStart Thrdstt = new ThreadStart(Thread2); Thread tt = new Thread(Thrdstt); tt.Start(); ThreadStart Thrdsttt = new ThreadStart(Thread3); Thread ttt = new Thread(Thrdsttt); ttt.Start(); } catch (Exception err) { label1.Text = err.Message.ToString(); } //Spawnning 3 Threads on form load.. private void Thread1() { Thread1 t1 = new Thread1(); t1.ThreadWriter(sw); } private void Thread2() { Thread2 t2 = new Thread2(); t2.ThreadWriter(sw); } private void Thread3() { Thread3 t3 = new Thread3(); t3.ThreadWriter(sw); } ------------------------------------------------------------------------------------------------------------------------- And i have three diferent clasees which write to text file... class Thread1 { private Object thisLock; public void ThreadWriter(StreamWriter sw) { thisLock = new Object(); lock (this) { for (int i = 0; i < 10; i++) { sw.WriteLine("Thread 1 = " + i); Thread.Sleep(100); } sw.Close(); } } } The other two are almost similar... Output is:- -------------- Thread 1 = 0 Thread 2 = 0 Thread 3 = 0 Thread 1 = 1 Thread 3 = 1 Thread 2 = 1 Thread 1 = 2 Thread 3 = 2 Thread 2 = 2 Thread 1 = 3 Thread 3 = 3 Thread 2 = 3 Thread 1 = 4 Thread 3 = 4 Thread 2 = 4 Thread 1 = 5 Thread 2 = 5 Thread 3 = 5 Thread 1 = 6 Thread 3 = 6 Thread 2 = 6 Thread 1 = 7 Thread 3 = 7 Thread 2 = 7 Thread 1 = 8 Thread 3 = 8 Thread 2 = 8 Thread 1 = 9 Thread 3 = 9 Thread 2 = 9 Please help me out.. Thanks in Advance..
You're going to have to explain what you mean by "not working". What are you expecting it to do because the output you got is what I expect, given the code you posted.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
thanks for ur reply...I have tried it with lock(object) but it is not working... Iam sending my code please go though it once and let me know, where i went wrong. private void Form1_Load(object sender, EventArgs e) { try { sw = new StreamWriter("D:\\Threading.txt"); ThreadStart Thrdst = new ThreadStart(Thread1); Thread t = new Thread(Thrdst); t.Start(); ThreadStart Thrdstt = new ThreadStart(Thread2); Thread tt = new Thread(Thrdstt); tt.Start(); ThreadStart Thrdsttt = new ThreadStart(Thread3); Thread ttt = new Thread(Thrdsttt); ttt.Start(); } catch (Exception err) { label1.Text = err.Message.ToString(); } //Spawnning 3 Threads on form load.. private void Thread1() { Thread1 t1 = new Thread1(); t1.ThreadWriter(sw); } private void Thread2() { Thread2 t2 = new Thread2(); t2.ThreadWriter(sw); } private void Thread3() { Thread3 t3 = new Thread3(); t3.ThreadWriter(sw); } ------------------------------------------------------------------------------------------------------------------------- And i have three diferent clasees which write to text file... class Thread1 { private Object thisLock; public void ThreadWriter(StreamWriter sw) { thisLock = new Object(); lock (this) { for (int i = 0; i < 10; i++) { sw.WriteLine("Thread 1 = " + i); Thread.Sleep(100); } sw.Close(); } } } The other two are almost similar... Output is:- -------------- Thread 1 = 0 Thread 2 = 0 Thread 3 = 0 Thread 1 = 1 Thread 3 = 1 Thread 2 = 1 Thread 1 = 2 Thread 3 = 2 Thread 2 = 2 Thread 1 = 3 Thread 3 = 3 Thread 2 = 3 Thread 1 = 4 Thread 3 = 4 Thread 2 = 4 Thread 1 = 5 Thread 2 = 5 Thread 3 = 5 Thread 1 = 6 Thread 3 = 6 Thread 2 = 6 Thread 1 = 7 Thread 3 = 7 Thread 2 = 7 Thread 1 = 8 Thread 3 = 8 Thread 2 = 8 Thread 1 = 9 Thread 3 = 9 Thread 2 = 9 Please help me out.. Thanks in Advance..
Your code is not synchronizing anything. Look:
class Thread1
{
private Object thisLock;public void ThreadWriter(StreamWriter sw) { thisLock = new Object(); lock (this) { for (int i = 0; i < 10; i++) { sw.WriteLine("Thread 1 = " + i); Thread.Sleep(100); } sw.Close(); } }
}
Your
thisLock
object is completely useless: You create a new instance of it withinThreadWriter
method but use it for nothing else. Using it as the argument forlock
would change nothing becouse of two reasons: you create a new instance of it every time the method runs, so you would always be loosing the previously locked reference, and it is private, so the other classes cannot use it for their locks. Usinglock(this)
is useless as well, becouse each class just locks itself for accessing theStreamWriter
more than once at a time, but does not lock the other classes since the other classes are doing the same, I mean, each one uses a different reference for their respective locks. What I still don't know is if you want all of your threads to be able to write at the same time in a thread safe way, or if you want only one thread writting everything it has to write and the other ones waiting. What kind of output would you like to get? -
Your code is not synchronizing anything. Look:
class Thread1
{
private Object thisLock;public void ThreadWriter(StreamWriter sw) { thisLock = new Object(); lock (this) { for (int i = 0; i < 10; i++) { sw.WriteLine("Thread 1 = " + i); Thread.Sleep(100); } sw.Close(); } }
}
Your
thisLock
object is completely useless: You create a new instance of it withinThreadWriter
method but use it for nothing else. Using it as the argument forlock
would change nothing becouse of two reasons: you create a new instance of it every time the method runs, so you would always be loosing the previously locked reference, and it is private, so the other classes cannot use it for their locks. Usinglock(this)
is useless as well, becouse each class just locks itself for accessing theStreamWriter
more than once at a time, but does not lock the other classes since the other classes are doing the same, I mean, each one uses a different reference for their respective locks. What I still don't know is if you want all of your threads to be able to write at the same time in a thread safe way, or if you want only one thread writting everything it has to write and the other ones waiting. What kind of output would you like to get?Thanks dude.. I will try it once again.. see all iam doing here is writing the content to stream file form mutliple threads. I spawned 3 threads, they will be writing data into a stream file. I just want to syncronize these three Threads while writing ... Thats all...
-
Thanks dude.. I will try it once again.. see all iam doing here is writing the content to stream file form mutliple threads. I spawned 3 threads, they will be writing data into a stream file. I just want to syncronize these three Threads while writing ... Thats all...
Ok, try this:
class Thread1
{
public void ThreadWriter(StreamWriter sw)
{for (int i = 0; i < 10; i++) { lock(sw) sw.WriteLine("Thread 1 = " + i); } }
}
Here each thread locks the
StreamWriter
object just before writing one line, and releases it just after doing it, so another thread can acquire the lock. This way a WriteLine operaton performed on one thread will never interfere a WriteLine operation of another thread. This is what I meant when I said "writing in a thread safe way".modified on Thursday, November 18, 2010 10:33 AM
-
Ok, try this:
class Thread1
{
public void ThreadWriter(StreamWriter sw)
{for (int i = 0; i < 10; i++) { lock(sw) sw.WriteLine("Thread 1 = " + i); } }
}
Here each thread locks the
StreamWriter
object just before writing one line, and releases it just after doing it, so another thread can acquire the lock. This way a WriteLine operaton performed on one thread will never interfere a WriteLine operation of another thread. This is what I meant when I said "writing in a thread safe way".modified on Thursday, November 18, 2010 10:33 AM
Thank u...