mirror of https://github.com/dahall/Vanara.git
Fixed disposal bug in BasicMessageWindow
parent
93efa5648e
commit
3109a2ebbf
|
@ -24,7 +24,7 @@ namespace Vanara.PInvoke
|
||||||
/// <seealso cref="Vanara.PInvoke.IHandle"/>
|
/// <seealso cref="Vanara.PInvoke.IHandle"/>
|
||||||
public class BasicMessageWindow : MarshalByRefObject, IDisposable, IHandle
|
public class BasicMessageWindow : MarshalByRefObject, IDisposable, IHandle
|
||||||
{
|
{
|
||||||
private SafeHWND hwnd;
|
private readonly SafeHWND hwnd;
|
||||||
private bool isDisposed;
|
private bool isDisposed;
|
||||||
|
|
||||||
/// <summary>Initializes a new instance of the <see cref="BasicMessageWindow"/> class.</summary>
|
/// <summary>Initializes a new instance of the <see cref="BasicMessageWindow"/> class.</summary>
|
||||||
|
@ -32,7 +32,7 @@ namespace Vanara.PInvoke
|
||||||
public BasicMessageWindow(BasicMessageWindowFilter callback = null)
|
public BasicMessageWindow(BasicMessageWindowFilter callback = null)
|
||||||
{
|
{
|
||||||
MessageFilter = callback;
|
MessageFilter = callback;
|
||||||
ClassName = $"MessageWindowBase+{Guid.NewGuid()}";
|
ClassName = $"{GetType().Name}+{Guid.NewGuid()}";
|
||||||
|
|
||||||
hwnd = CreateWindow();
|
hwnd = CreateWindow();
|
||||||
}
|
}
|
||||||
|
@ -68,9 +68,10 @@ namespace Vanara.PInvoke
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected virtual SafeHWND CreateWindow()
|
protected virtual SafeHWND CreateWindow()
|
||||||
{
|
{
|
||||||
if (0 == RegisterClassEx(new WNDCLASSEX { cbSize = (uint)Marshal.SizeOf(typeof(WNDCLASSEX)), lpfnWndProc = WndProc, hInstance = GetModuleHandle(), lpszClassName = ClassName }))
|
var hInst = GetModuleHandle();
|
||||||
Win32Error.ThrowLastError();
|
var wcx = new WNDCLASSEX { cbSize = (uint)Marshal.SizeOf(typeof(WNDCLASSEX)), lpfnWndProc = WndProc, hInstance = hInst, lpszClassName = ClassName };
|
||||||
return Win32Error.ThrowLastErrorIfInvalid(CreateWindowEx(lpClassName: ClassName, lpWindowName: ClassName, hWndParent: HWND.HWND_MESSAGE));
|
var atom = Win32Error.ThrowLastErrorIfNull(Macros.MAKEINTATOM(RegisterClassEx(wcx)));
|
||||||
|
return Win32Error.ThrowLastErrorIfInvalid(CreateWindowEx(lpClassName: atom, hWndParent: HWND.HWND_MESSAGE, hInstance: hInst));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Releases unmanaged and - optionally - managed resources.</summary>
|
/// <summary>Releases unmanaged and - optionally - managed resources.</summary>
|
||||||
|
@ -81,27 +82,16 @@ namespace Vanara.PInvoke
|
||||||
{
|
{
|
||||||
if (isDisposed)
|
if (isDisposed)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
isDisposed = true;
|
isDisposed = true;
|
||||||
|
|
||||||
hwnd?.Dispose();
|
hwnd?.Dispose(); // Calls DestroyWindow
|
||||||
|
|
||||||
UnregisterClass(ClassName, GetModuleHandle());
|
UnregisterClass(ClassName, GetModuleHandle());
|
||||||
ClassName = null;
|
ClassName = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private IntPtr WndProc(HWND hwnd, uint msg, IntPtr wParam, IntPtr lParam)
|
private IntPtr WndProc(HWND hwnd, uint msg, IntPtr wParam, IntPtr lParam) =>
|
||||||
{
|
MessageFilter is null || !MessageFilter.Invoke(hwnd, msg, wParam, lParam, out var lRet)
|
||||||
IntPtr ret;
|
? DefWindowProc(hwnd, msg, wParam, lParam)
|
||||||
if (!(MessageFilter is null) && MessageFilter.Invoke(hwnd, msg, wParam, lParam, out var lRet))
|
: lRet;
|
||||||
ret = lRet;
|
|
||||||
else
|
|
||||||
ret = DefWindowProc(hwnd, msg, wParam, lParam);
|
|
||||||
|
|
||||||
if (msg == (uint)WindowMessage.WM_NCDESTROY)
|
|
||||||
Dispose(true);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue