public ResourceUser() { handle = APIFunctionSimulator.GetResource(); if (handle.ToInt32() == 0) { throw new ApplicationException(); } }
public void UseResource() { if (handle.ToInt32() == 0) { throw new ObjectDisposedException("handle used in UseResource class after object disposed"); } string result = APIFunctionSimulator.UseResource(handle); Console.WriteLine("In ResourceUser.UseResource, result is :" + result); }
public void Dispose() { lock (this) { if (handle.ToInt32() != 0) { APIFunctionSimulator.ReleaseResource(handle); handle = (IntPtr)0; GC.SuppressFinalize(this); } } }
static void UseUnmanagedResource() { IntPtr handle = APIFunctionSimulator.GetResource(); try { string result = APIFunctionSimulator.UseResource(handle); Console.WriteLine("In EntryPoint.UseUnmanagedResource, result is :" + result); } catch (Exception e) { Console.WriteLine("Exception in UseUnmanagedResource: " + e.Message); } finally { if (handle.ToInt32() != 0) { APIFunctionSimulator.ReleaseResource(handle); } } }