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. Concept of Thread.BeginCriticalRegion()

Concept of Thread.BeginCriticalRegion()

Scheduled Pinned Locked Moved C#
helpquestion
7 Posts 4 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.
  • S Offline
    S Offline
    Sandeep Akhare
    wrote on last edited by
    #1

    HI All I am confused while using Thread.BeginCriticalRegion(); in my application what i am trying after compliting specific task by thread i have to abort same thread by calling abort method see the code private void button3_Click(object sender, EventArgs e) { ParameterizedThreadStart paramThreadDelagate = new ParameterizedThreadStart(WorkWithParameter); Thread thread = new Thread(paramThreadDelagate); thread.Start(); Thread.Sleep(1000); thread.Abort(); MessageBox.Show("Aborted"); } private void WorkWithParameter() { Thread.BeginCriticalRegion(); MessageBox.Show("1"); MessageBox.Show("2"); MessageBox.Show("3"); MessageBox.Show("4"); MessageBox.Show("5"); Thread.EndCriticalRegion(); } The idea behind the critical region is to provide a region of code that must be executed as if it were a single statement. Any attempt to abort a thread while a critical region will have to wait until after the critical region is compplite .so the problem is i am getting message Aborted instead of executing whole WorkWithParameter method. Can any one help me out ?

    Thanks and Regards Sandeep If you want something you never had, do something you have never done!

    J P S 3 Replies Last reply
    0
    • S Sandeep Akhare

      HI All I am confused while using Thread.BeginCriticalRegion(); in my application what i am trying after compliting specific task by thread i have to abort same thread by calling abort method see the code private void button3_Click(object sender, EventArgs e) { ParameterizedThreadStart paramThreadDelagate = new ParameterizedThreadStart(WorkWithParameter); Thread thread = new Thread(paramThreadDelagate); thread.Start(); Thread.Sleep(1000); thread.Abort(); MessageBox.Show("Aborted"); } private void WorkWithParameter() { Thread.BeginCriticalRegion(); MessageBox.Show("1"); MessageBox.Show("2"); MessageBox.Show("3"); MessageBox.Show("4"); MessageBox.Show("5"); Thread.EndCriticalRegion(); } The idea behind the critical region is to provide a region of code that must be executed as if it were a single statement. Any attempt to abort a thread while a critical region will have to wait until after the critical region is compplite .so the problem is i am getting message Aborted instead of executing whole WorkWithParameter method. Can any one help me out ?

      Thanks and Regards Sandeep If you want something you never had, do something you have never done!

      J Offline
      J Offline
      Jimmanuel
      wrote on last edited by
      #2

      I'm not sure that BeginCriticalRegion does what you think it does. MSDN[^] says this about the function:

      Notifies a host that execution is about to enter a region of code in which the effects of a thread abort or unhandled exception might jeopardize other tasks in the application domain.

      It doesn't say anything about multiple lines of code being executed "as if it were a single statement", and doesn't suggest anywhere that it blocks Thread.Abort() from ending the Thread.

      S 1 Reply Last reply
      0
      • S Sandeep Akhare

        HI All I am confused while using Thread.BeginCriticalRegion(); in my application what i am trying after compliting specific task by thread i have to abort same thread by calling abort method see the code private void button3_Click(object sender, EventArgs e) { ParameterizedThreadStart paramThreadDelagate = new ParameterizedThreadStart(WorkWithParameter); Thread thread = new Thread(paramThreadDelagate); thread.Start(); Thread.Sleep(1000); thread.Abort(); MessageBox.Show("Aborted"); } private void WorkWithParameter() { Thread.BeginCriticalRegion(); MessageBox.Show("1"); MessageBox.Show("2"); MessageBox.Show("3"); MessageBox.Show("4"); MessageBox.Show("5"); Thread.EndCriticalRegion(); } The idea behind the critical region is to provide a region of code that must be executed as if it were a single statement. Any attempt to abort a thread while a critical region will have to wait until after the critical region is compplite .so the problem is i am getting message Aborted instead of executing whole WorkWithParameter method. Can any one help me out ?

        Thanks and Regards Sandeep If you want something you never had, do something you have never done!

        P Offline
        P Offline
        pbraun
        wrote on last edited by
        #3

        Look up "lock" or "Monitor" in your help. Phil

        S 1 Reply Last reply
        0
        • S Sandeep Akhare

          HI All I am confused while using Thread.BeginCriticalRegion(); in my application what i am trying after compliting specific task by thread i have to abort same thread by calling abort method see the code private void button3_Click(object sender, EventArgs e) { ParameterizedThreadStart paramThreadDelagate = new ParameterizedThreadStart(WorkWithParameter); Thread thread = new Thread(paramThreadDelagate); thread.Start(); Thread.Sleep(1000); thread.Abort(); MessageBox.Show("Aborted"); } private void WorkWithParameter() { Thread.BeginCriticalRegion(); MessageBox.Show("1"); MessageBox.Show("2"); MessageBox.Show("3"); MessageBox.Show("4"); MessageBox.Show("5"); Thread.EndCriticalRegion(); } The idea behind the critical region is to provide a region of code that must be executed as if it were a single statement. Any attempt to abort a thread while a critical region will have to wait until after the critical region is compplite .so the problem is i am getting message Aborted instead of executing whole WorkWithParameter method. Can any one help me out ?

          Thanks and Regards Sandeep If you want something you never had, do something you have never done!

          S Offline
          S Offline
          Stefan Prodan
          wrote on last edited by
          #4

          Thread.BeginCriticalRegion does not block the abort command, and by the way the first rule that I've learned about multi-threading is to never use Abort, there are tones of other ways to stop a thread gracefully. Here it is an alternative to Thread.Abort: http://stefanprodan.wordpress.com/2007/03/22/stop-threads-gracefuly[^]

          1 Reply Last reply
          0
          • J Jimmanuel

            I'm not sure that BeginCriticalRegion does what you think it does. MSDN[^] says this about the function:

            Notifies a host that execution is about to enter a region of code in which the effects of a thread abort or unhandled exception might jeopardize other tasks in the application domain.

            It doesn't say anything about multiple lines of code being executed "as if it were a single statement", and doesn't suggest anywhere that it blocks Thread.Abort() from ending the Thread.

            S Offline
            S Offline
            Sandeep Akhare
            wrote on last edited by
            #5

            Hi But it says that Thread.Abort does not make any effect if the current context is in the critical region thats why Dot.NET 2.0 has introduced this and Thread.Suspend and Thread.Resume have been retired Actually i m reading Microsoft press book name .NET FrameWork 2.0 Application Development Foundation by Tony Northup and many more in that book they mention " The idea behind the crical region is to provide a region of code that must be executed as if it were single statement . Any attempt to abort a thread while it is within a critical region will have to wait until after the critical region is complete ." PAge number 380 Chapter 7 lesson 1 .

            Thanks and Regards Sandeep If you want something you never had, do something you have never done!

            J 1 Reply Last reply
            0
            • P pbraun

              Look up "lock" or "Monitor" in your help. Phil

              S Offline
              S Offline
              Sandeep Akhare
              wrote on last edited by
              #6

              Lock and Monitor concept are for mulitythreading Synchronization concept Which are not different what i asked i think

              Thanks and Regards Sandeep If you want something you never had, do something you have never done!

              1 Reply Last reply
              0
              • S Sandeep Akhare

                Hi But it says that Thread.Abort does not make any effect if the current context is in the critical region thats why Dot.NET 2.0 has introduced this and Thread.Suspend and Thread.Resume have been retired Actually i m reading Microsoft press book name .NET FrameWork 2.0 Application Development Foundation by Tony Northup and many more in that book they mention " The idea behind the crical region is to provide a region of code that must be executed as if it were single statement . Any attempt to abort a thread while it is within a critical region will have to wait until after the critical region is complete ." PAge number 380 Chapter 7 lesson 1 .

                Thanks and Regards Sandeep If you want something you never had, do something you have never done!

                J Offline
                J Offline
                Jimmanuel
                wrote on last edited by
                #7

                Hmm . . . from that text it does sound like BeginCriticalRegion is supposed to create a non-Abortable section of code, but your own test program is showing that it doesn't. When the author uses the phrase "The idea behind the critical region" it makes me wonder exactly what he means - does he mean that the code IS executed as one statement (and is therefore not abortable), or that idealistically .Net tries to make it appear to the CLR that the code being executed is one statement? The answer seems to be shown by your test program. Check out this[^] post - it asks the same question that you have and Peter Ritchie [C# MVP] has done a good job of answering it.

                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