Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Threading in C#

Threading in C#

Scheduled Pinned Locked Moved C#
csharphelplearning
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anil Kumar Arvapalli
    wrote on last edited by
    #1

    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 :)

    A P 2 Replies Last reply
    0
    • A Anil Kumar Arvapalli

      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 :)

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      Check out msdn[^].

      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.

      A 1 Reply Last reply
      0
      • A Anil Kumar Arvapalli

        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 :)

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        Sacha Barber did an excellent series[^] on threading. I would suggest that you start there.

        I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

        Forgive your enemies - it messes with their heads

        My blog | My articles | MoXAML PowerToys | Onyx

        1 Reply Last reply
        0
        • A Abhinav S

          Check out msdn[^].

          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.

          A Offline
          A Offline
          Anil Kumar Arvapalli
          wrote on last edited by
          #4

          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..

          D _ 2 Replies Last reply
          0
          • A Anil Kumar Arvapalli

            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..

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • A Anil Kumar Arvapalli

              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..

              _ Offline
              _ Offline
              _Erik_
              wrote on last edited by
              #6

              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 within ThreadWriter method but use it for nothing else. Using it as the argument for lock 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. Using lock(this) is useless as well, becouse each class just locks itself for accessing the StreamWriter 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?

              A 1 Reply Last reply
              0
              • _ _Erik_

                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 within ThreadWriter method but use it for nothing else. Using it as the argument for lock 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. Using lock(this) is useless as well, becouse each class just locks itself for accessing the StreamWriter 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?

                A Offline
                A Offline
                Anil Kumar Arvapalli
                wrote on last edited by
                #7

                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...

                _ 1 Reply Last reply
                0
                • A Anil Kumar Arvapalli

                  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...

                  _ Offline
                  _ Offline
                  _Erik_
                  wrote on last edited by
                  #8

                  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

                  A 1 Reply Last reply
                  0
                  • _ _Erik_

                    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

                    A Offline
                    A Offline
                    Anil Kumar Arvapalli
                    wrote on last edited by
                    #9

                    Thank u...

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups