|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Atomic Operation?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
Show quote
Hide quote
> I was wondering if there is a simple way of ensuring that some There is no way to make them atomic in the sense you mean. You have two > 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? 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. 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: Show quoteHide quote > 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 HM wrote:
> I was wondering if there is a simple way of ensuring that some The "Visual Basic" way is to use the SyncLock keyword.> 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 full-blown Framework'y way would be a Mutex object. HTH, Phill W. Phill W. wrote:
> Joe HM wrote: Phill,> > The "Visual Basic" way is to use the SyncLock keyword. > > The full-blown Framework'y way would be a Mutex object. > > HTH, > Phill W. 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
Show quote
Hide quote
"Joe HM" <unixve***@yahoo.com> schrieb: \\\> 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/> Joe HM wrote:
> Hello - Use SyncLock or Monitor.Enter and Monitor.Exit.> > 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 ... Where is this executing? I'm assuming it's on a thread other than the> > Dim mAppDomain As AppDomain > > The following sets the mAppDomain in a function ... > mAppDomain = AppDomain.CreateDomain(lFullPathAssembly) > ... and does other stuff with it ... > main UI thread. Is that correct? Show quoteHide quote > The problem is when for whatever reason the GUI is closed while the The only way that could happen is if the previous code snippet you> 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. > provided is executing on another thread. > Is there a way to make the If() and Unload() calls atomic so that Yes, you must wrap both code snippets with a lock by using the SyncLock> nothing else can be done with mAppDomain in between those calls? > keyword. Its not guarenteed to work if you only wrap the contents of the OnClosing method. Show quoteHide quote > Thanks! > Joe 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: Show quoteHide quote > 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
Saving changes made in DataGridView
One Last Class Question - Example that works DirectX rendered inside a control? problem save and reading from the registry Database Using SQLDataSource without a control? Problems with SaveAs Current namespace, stack and lineno How to make double click event to call and execute the mouseup event code I/O Access through window handle? |
|||||||||||||||||||||||