473,322 Members | 1,703 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,322 software developers and data experts.

Atomic Operation?

Hello -

I was wondering if there is a simple way of ensuring that some
statements are executed as an "atomic operation". Here is what I am
dealing with in a GUI ...

Dim mAppDomain As AppDomain

The following sets the mAppDomain in a function ...
mAppDomain = AppDomain.CreateDomain(lFullPathAssembly)
.... and does other stuff with it ...

The problem is when for whatever reason the GUI is closed while the
above is running and the above causes an exception that sets the
mAppDomain to Nothing.

Protected Overrides OnClosing(...)
If Not (mAppDomain Is Nothing) Then
AppDomain.Unload(mAppDomain)
mAppDomain = Nothing
End If
...

What could potentially happen is that the mAppDomain is set to Nothing
between the If Not () Then and the Unload() call.

Is there a way to make the If() and Unload() calls atomic so that
nothing else can be done with mAppDomain in between those calls?

Thanks!
Joe

Aug 25 '06 #1
7 3555
I was wondering if there is a simple way of ensuring that some
statements are executed as an "atomic operation". Here is what I am
dealing with in a GUI ...

Dim mAppDomain As AppDomain

The following sets the mAppDomain in a function ...
mAppDomain = AppDomain.CreateDomain(lFullPathAssembly)
.... and does other stuff with it ...

The problem is when for whatever reason the GUI is closed while the
above is running and the above causes an exception that sets the
mAppDomain to Nothing.

Protected Overrides OnClosing(...)
If Not (mAppDomain Is Nothing) Then
AppDomain.Unload(mAppDomain)
mAppDomain = Nothing
End If
...

What could potentially happen is that the mAppDomain is set to Nothing
between the If Not () Then and the Unload() call.

Is there a way to make the If() and Unload() calls atomic so that
nothing else can be done with mAppDomain in between those calls?
There is no way to make them atomic in the sense you mean. You have two
choices. First, you can guard mAppDomain with a mutex. In all places in
code where mAppDomain is referenced, acquire the mutex, do your thing, and
release the mutex. Second, you could put the code you are worried about in a
Try-Catch block, and if the exception is the null reference exception, you
ought to be able to clean up in Catch without causing any downstream problems.
Aug 25 '06 #2
Joe HM wrote:
I was wondering if there is a simple way of ensuring that some
statements are executed as an "atomic operation".
What could potentially happen is that the mAppDomain is set to Nothing
between the If Not () Then and the Unload() call.
Is there a way to make the If() and Unload() calls atomic so that
nothing else can be done with mAppDomain in between those calls?
The "Visual Basic" way is to use the SyncLock keyword.

The full-blown Framework'y way would be a Mutex object.

HTH,
Phill W.
Aug 25 '06 #3
Hello -

Yeah ... I figured I could use Try/Catch in that case. I will look
into it some more and maybe use a Mutex if necessary.

Thanks guys!
Joe
AMercer wrote:
There is no way to make them atomic in the sense you mean. You have two
choices. First, you can guard mAppDomain with a mutex. In all places in
code where mAppDomain is referenced, acquire the mutex, do your thing, and
release the mutex. Second, you could put the code you are worried about in a
Try-Catch block, and if the exception is the null reference exception, you
ought to be able to clean up in Catch without causing any downstream problems.
Aug 25 '06 #4
"Joe HM" <un*******@yahoo.comschrieb:
I was wondering if there is a simple way of ensuring that some
statements are executed as an "atomic operation". Here is what I am
dealing with in a GUI ...

Dim mAppDomain As AppDomain

The following sets the mAppDomain in a function ...
mAppDomain = AppDomain.CreateDomain(lFullPathAssembly)
... and does other stuff with it ...

The problem is when for whatever reason the GUI is closed while the
above is running and the above causes an exception that sets the
mAppDomain to Nothing.

Protected Overrides OnClosing(...)
If Not (mAppDomain Is Nothing) Then
AppDomain.Unload(mAppDomain)
mAppDomain = Nothing
End If
...

What could potentially happen is that the mAppDomain is set to Nothing
between the If Not () Then and the Unload() call.

Is there a way to make the If() and Unload() calls atomic so that
nothing else can be done with mAppDomain in between those calls?
\\\
Private m_LockObject As New Object()

Protected Overrides Sub OnClosing(...)
SyncLock m_LockObject
If Not (mAppDomain Is Nothing) Then
AppDomain.Unload(mAppDomain)
mAppDomain = Nothing
End If
End SyncLock
End Sub
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Aug 25 '06 #5

Joe HM wrote:
Hello -

I was wondering if there is a simple way of ensuring that some
statements are executed as an "atomic operation".
Use SyncLock or Monitor.Enter and Monitor.Exit.
Here is what I am dealing with in a GUI ...

Dim mAppDomain As AppDomain

The following sets the mAppDomain in a function ...
mAppDomain = AppDomain.CreateDomain(lFullPathAssembly)
... and does other stuff with it ...
Where is this executing? I'm assuming it's on a thread other than the
main UI thread. Is that correct?
The problem is when for whatever reason the GUI is closed while the
above is running and the above causes an exception that sets the
mAppDomain to Nothing.

Protected Overrides OnClosing(...)
If Not (mAppDomain Is Nothing) Then
AppDomain.Unload(mAppDomain)
mAppDomain = Nothing
End If
...

What could potentially happen is that the mAppDomain is set to Nothing
between the If Not () Then and the Unload() call.
The only way that could happen is if the previous code snippet you
provided is executing on another thread.
Is there a way to make the If() and Unload() calls atomic so that
nothing else can be done with mAppDomain in between those calls?
Yes, you must wrap both code snippets with a lock by using the SyncLock
keyword. Its not guarenteed to work if you only wrap the contents of
the OnClosing method.
Thanks!
Joe
Aug 25 '06 #6

Phill W. wrote:
Joe HM wrote:

The "Visual Basic" way is to use the SyncLock keyword.

The full-blown Framework'y way would be a Mutex object.

HTH,
Phill W.
Phill,

Actually, SyncLock is the "full-blown Framework'y" way of doing it. A
Mutex is typically used to synchronize access to a resource shared by
multiple processes.

Brian

Aug 25 '06 #7
Hello -

Thanks for the feedback. Yes ... I am using multiple threads. I will
give SyncLock or Monitor.Enter/Exit a try.

Thanks!
Joe
Brian Gideon wrote:
Joe HM wrote:
Hello -

I was wondering if there is a simple way of ensuring that some
statements are executed as an "atomic operation".

Use SyncLock or Monitor.Enter and Monitor.Exit.
Here is what I am dealing with in a GUI ...

Dim mAppDomain As AppDomain

The following sets the mAppDomain in a function ...
mAppDomain = AppDomain.CreateDomain(lFullPathAssembly)
... and does other stuff with it ...

Where is this executing? I'm assuming it's on a thread other than the
main UI thread. Is that correct?
The problem is when for whatever reason the GUI is closed while the
above is running and the above causes an exception that sets the
mAppDomain to Nothing.

Protected Overrides OnClosing(...)
If Not (mAppDomain Is Nothing) Then
AppDomain.Unload(mAppDomain)
mAppDomain = Nothing
End If
...

What could potentially happen is that the mAppDomain is set to Nothing
between the If Not () Then and the Unload() call.

The only way that could happen is if the previous code snippet you
provided is executing on another thread.
Is there a way to make the If() and Unload() calls atomic so that
nothing else can be done with mAppDomain in between those calls?

Yes, you must wrap both code snippets with a lock by using the SyncLock
keyword. Its not guarenteed to work if you only wrap the contents of
the OnClosing method.
Thanks!
Joe
Aug 30 '06 #8

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

Similar topics

5
by: Paul Moore | last post by:
I can't find anything which spells this out in the manuals. I guess that, at some level, the answer is "a single bytecode operation", but I'm not sure that explains it for me. This thought was...
42
by: Shayan | last post by:
Is there a boolean flag that can be set atomically without needing to wrap it in a mutex? This flag will be checked constantly by multiple threads so I don't really want to deal with the overhead...
3
by: Zeng | last post by:
Hello, Is the assignment operation atomic? That is if one thread assigns to static variable an object and another uses the object assigned to the variable, would it be safe? Thanks! zeng
6
by: blackstreetcat | last post by:
consider this code : int i; //gobal var Thread1: i=some value; Thread2: if (i==2) dosomething(); else dosomethingelse();
1
by: spacehopper_man | last post by:
no "rename" operation in C# !!! - this has been covered in this group before, but I can't find any good answers. what I am trying to do is refresh the content in a file with minimum...
11
by: japhy | last post by:
Is there a way to read a line (a series of characters ending in a newline) from a file (either by descriptor or stream) atomically, without buffering additional contents of the file?
2
by: Freedom fighter | last post by:
Hello, Is a singleton class the same as an atomic class? I know that a singleton class can only be instantiated once, but does that concept apply to an atomic class? Thank you.
10
by: Dmitriy V'jukov | last post by:
On 16 ÍÁÊ, 21:41, "Dmitriy V'jukov" <dvyu...@gmail.comwrote: According to definition, release operation on 'std::atomic_global_fence_compatibility' can only 'synchronize with' acquire...
11
by: Jon Harrop | last post by:
Can read locks on a data structure be removed safely when updates are limited to replacing a reference? In other words, is setting a reference an atomic operation? I have been assuming that all...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.