Concept of Thread.BeginCriticalRegion()
-
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!
-
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!
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 theThread
. -
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!
-
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!
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[^]
-
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 theThread
.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!
-
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!
-
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!
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.