473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

BeginCriticalRegion - EndCriticalRegion

Hi,

As I understand, if a code section inside a sub called with threadX.Start
is between BeginCriticalRegion - EndCriticalRegion, it should complete (at
least that portion of code, not the whole 'sub'... even if a TreadX.Abort is
used, if this portion of code is running when you call that Abort.

So With this program, I would expect to print :

I'm still standing, yeh yeh yéah
Main thread finished

But only

Main thread finished

appears because thread is aborted although The code between critical region
is running... Why ?

Code:
Sub Main()
Dim myThreadx As New Thread(AddressOf subWithASleep)
myThreadx.Start()
Thread.Sleep(2000) 'For the called thread to have some time to enter that
"begin-critical-region"

myThreadx.Abort()
Console.WriteLine("Main thread finished")

end sub
Public Sub subWithASleep()

Thread.BeginCriticalRegion()

Thread.Sleep(15000) 'waits 15 seconds
Console.Write("I'm still standing, yeh yeh yéah") 'It should show
but it does NOT !

Thread.EndCriticalRegion()

End Sub
--
Roger
..NET 2005 and DB developer
Apr 14 '07 #1
2 4599
Are you confusing Critical Regions and Constrained Execution Regions?

BeginCriticalRegion/EndCriticalRegion is used to signify a critical region
of code--a region of code that non-atomically modifies data shared with other
threads. Interruption of code in critical region of code generally would
affect other threads. BeginCriticalRegion/EndCriticalRegion don't stop code
from being interrupted; it allows the host to decide what to do if a critical
region does get interrupted (like unload the AppDomain the code is in).

Manually specifying critical regions when using Monitor (and hence the
lock/SyncLock keywords) or Mutex appears largely redundant as Monitor and
Mutex already do this (as described by Jeffery Richter [1]; although I can
find no Microsoft documentation on this).

Are you trying to write a block of code that can't be interrupted?

[1]
http://msdn.microsoft.com/msdnmag/is...s/default.aspx
--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"Roger" wrote:
Hi,

As I understand, if a code section inside a sub called with threadX.Start
is between BeginCriticalRegion - EndCriticalRegion, it should complete (at
least that portion of code, not the whole 'sub'... even if a TreadX.Abort is
used, if this portion of code is running when you call that Abort.

So With this program, I would expect to print :

I'm still standing, yeh yeh yéah
Main thread finished

But only

Main thread finished

appears because thread is aborted although The code between critical region
is running... Why ?

Code:
Sub Main()
Dim myThreadx As New Thread(AddressOf subWithASleep)
myThreadx.Start()
Thread.Sleep(2000) 'For the called thread to have some time to enter that
"begin-critical-region"

myThreadx.Abort()
Console.WriteLine("Main thread finished")

end sub
Public Sub subWithASleep()

Thread.BeginCriticalRegion()

Thread.Sleep(15000) 'waits 15 seconds
Console.Write("I'm still standing, yeh yeh yéah") 'It should show
but it does NOT !

Thread.EndCriticalRegion()

End Sub
Apr 15 '07 #2
Thanks, Peter. I'll take a deeper look at this.

Bye,

--
Roger Tranchez
..NET 2005 and DB developer
"Peter Ritchie [C# MVP]" wrote:
Are you confusing Critical Regions and Constrained Execution Regions?

BeginCriticalRegion/EndCriticalRegion is used to signify a critical region
of code--a region of code that non-atomically modifies data shared with other
threads. Interruption of code in critical region of code generally would
affect other threads. BeginCriticalRegion/EndCriticalRegion don't stop code
from being interrupted; it allows the host to decide what to do if a critical
region does get interrupted (like unload the AppDomain the code is in).

Manually specifying critical regions when using Monitor (and hence the
lock/SyncLock keywords) or Mutex appears largely redundant as Monitor and
Mutex already do this (as described by Jeffery Richter [1]; although I can
find no Microsoft documentation on this).

Are you trying to write a block of code that can't be interrupted?

[1]
http://msdn.microsoft.com/msdnmag/is...s/default.aspx
--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"Roger" wrote:
Hi,

As I understand, if a code section inside a sub called with threadX.Start
is between BeginCriticalRegion - EndCriticalRegion, it should complete (at
least that portion of code, not the whole 'sub'... even if a TreadX.Abort is
used, if this portion of code is running when you call that Abort.

So With this program, I would expect to print :

I'm still standing, yeh yeh yéah
Main thread finished

But only

Main thread finished

appears because thread is aborted although The code between critical region
is running... Why ?

Code:
Sub Main()
Dim myThreadx As New Thread(AddressOf subWithASleep)
myThreadx.Start()
Thread.Sleep(2000) 'For the called thread to have some time to enter that
"begin-critical-region"

myThreadx.Abort()
Console.WriteLine("Main thread finished")

end sub
Public Sub subWithASleep()

Thread.BeginCriticalRegion()

Thread.Sleep(15000) 'waits 15 seconds
Console.Write("I'm still standing, yeh yeh yéah") 'It should show
but it does NOT !

Thread.EndCriticalRegion()

End Sub
May 28 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: joe.on.threads | last post by:
I've written an extensive document on threading in C#: http://www.albahari.com/threading It started out as a chapter in a book that I'm writing, but ended up outgrowing the book! So I'm making...
0
by: Carly | last post by:
Hi, I have the following code that uses beginCriticalRegion and EndCriticalRegion Imports System.Threading Public Class Form1 Shared Sub SimpleWorks(ByVal o As Object) Dim info As person =...
1
by: =?Utf-8?B?Qi4gQ2hlcm5pY2s=?= | last post by:
I'm trying to study for a cert and I'm writing a few examples loosely based on Chapter 7 of the 536 manual. I am not used to thread programming. The manual states that: 'Any attempt to abort a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.