Atomic Operation?

  • Thread starter Thread starter Joe HM
  • Start date Start date
J

Joe HM

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
 
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.
 
Joe said:
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.
 
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
 
Joe HM said:
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
///
 
Joe said:
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.
 
Phill said:
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
 
Hello -

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

Thanks!
Joe
 
Back
Top