public void ThreadContext_EmptyProcessFiltersWorks() { // Test that no filters at all does not throw, and that returns false from translation Application.ThreadContext threadContext = new Application.ThreadContext(); var msg = new User32.MSG(); Assert.False(threadContext.PreTranslateMessage(ref msg)); }
public void ThreadContext_EmptyProcessFiltersWorks() { // Test that no filters at all does not throw, and that returns false from translation Application.ThreadContext threadContext = new Application.ThreadContext(); NativeMethods.MSG msg = new NativeMethods.MSG(); bool result = threadContext.PreTranslateMessage(ref msg); Assert.False(result); }
public WindowsFormsSynchronizationContext() { this.DestinationThread = Thread.CurrentThread; Application.ThreadContext context = Application.ThreadContext.FromCurrent(); if (context != null) { this.controlToSendTo = context.MarshalingControl; } }
/// <summary> /// This was factored into another function so the finalizer in control that releases the window /// can perform the exact same code without further changes. If you make changes to the finalizer, /// change this method -- try not to change NativeWindow's finalizer. /// </summary> internal void ForceExitMessageLoop() { IntPtr handle; bool ownedHandle; lock (this) { handle = Handle; ownedHandle = _ownHandle; } if (handle != IntPtr.Zero) { // Now, before we set handle to zero and finish the finalizer, let's send // a WM_NULL to the window. Why? Because if the main ui thread is INSIDE // the wndproc for this control during our unsubclass, then we could AV // when control finally reaches us. if (User32.IsWindow(handle).IsTrue()) { uint id = User32.GetWindowThreadProcessId(handle, out uint lpdwProcessId); Application.ThreadContext ctx = Application.ThreadContext.FromId(id); IntPtr threadHandle = (ctx == null ? IntPtr.Zero : ctx.GetHandle()); if (threadHandle != IntPtr.Zero) { Kernel32.GetExitCodeThread(threadHandle, out uint exitCode); if (!AppDomain.CurrentDomain.IsFinalizingForUnload() && exitCode == NativeMethods.STATUS_PENDING) { if (User32.SendMessageTimeoutW( handle, User32.RegisteredMessage.WM_UIUNSUBCLASS, IntPtr.Zero, IntPtr.Zero, User32.SMTO.ABORTIFHUNG, 100, out _) == IntPtr.Zero) { //Debug.Fail("unable to ping HWND:" + handle.ToString() + " during finalization"); } } } } if (Handle != IntPtr.Zero) { // If the dest thread is gone, it should be safe to unsubclass here. ReleaseHandle(true); } } if (handle != IntPtr.Zero && ownedHandle) { // If we owned the handle, post a WM_CLOSE to get rid of it. User32.PostMessageW(handle, User32.WindowMessage.WM_CLOSE); } }
/// <include file='doc\WindowsFormsSynchronizationContext.uex' path='docs/doc[@for="WindowsFormsSynchronizationContext.WindowsFormsSynchronizationContext"]/*' /> public WindowsFormsSynchronizationContext() { DestinationThread = Thread.CurrentThread; //store the current thread to ensure its still alive during an invoke. Application.ThreadContext context = Application.ThreadContext.FromCurrent(); Debug.Assert(context != null); if (context != null) { controlToSendTo = context.MarshalingControl; } Debug.Assert(controlToSendTo.IsHandleCreated, "Marshaling control should have created its handle in its ctor."); }
public void ThreadContext_MultipleProcessFiltersProcesses() { // Test that multiple filters work Application.ThreadContext threadContext = new Application.ThreadContext(); int filterId2 = TestMessageId2; var mockContext2 = new Mock <IMessageFilter>(MockBehavior.Strict); mockContext2.Setup(c => c.PreFilterMessage(ref It.Ref <Message> .IsAny)) .Returns((MessageCallback)((ref Message m) => m.Msg == filterId2)); threadContext.AddMessageFilter(mockContext2.Object); int filterId3 = TestMessageId3; var mockContext3 = new Mock <IMessageFilter>(MockBehavior.Strict); mockContext3.Setup(c => c.PreFilterMessage(ref It.Ref <Message> .IsAny)) .Returns((MessageCallback)((ref Message m) => m.Msg == filterId3)); threadContext.AddMessageFilter(mockContext3.Object); NativeMethods.MSG msg = new NativeMethods.MSG { message = TestMessageId1 }; bool result = threadContext.PreTranslateMessage(ref msg); Assert.False(result); mockContext2.Verify(c => c.PreFilterMessage(ref It.Ref <Message> .IsAny), Times.Exactly(1)); mockContext3.Verify(c => c.PreFilterMessage(ref It.Ref <Message> .IsAny), Times.Exactly(1)); msg = new NativeMethods.MSG { message = TestMessageId2 }; result = threadContext.PreTranslateMessage(ref msg); Assert.True(result); mockContext2.Verify(c => c.PreFilterMessage(ref It.Ref <Message> .IsAny), Times.Exactly(2)); mockContext3.Verify(c => c.PreFilterMessage(ref It.Ref <Message> .IsAny), Times.Exactly(1)); msg = new NativeMethods.MSG { message = TestMessageId3 }; result = threadContext.PreTranslateMessage(ref msg); Assert.True(result); mockContext2.Verify(c => c.PreFilterMessage(ref It.Ref <Message> .IsAny), Times.Exactly(3)); mockContext3.Verify(c => c.PreFilterMessage(ref It.Ref <Message> .IsAny), Times.Exactly(2)); }
public void ThreadContext_CorrectProcessFiltersProcesses() { // Test that a filter with the correct ID returns true Application.ThreadContext threadContext = new Application.ThreadContext(); int filterId = TestMessageId2; var mockContext = new Mock <IMessageFilter>(MockBehavior.Strict); mockContext.Setup(c => c.PreFilterMessage(ref It.Ref <Message> .IsAny)) .Returns((MessageCallback)((ref Message m) => m.Msg == filterId)); threadContext.AddMessageFilter(mockContext.Object); NativeMethods.MSG msg = new NativeMethods.MSG(); msg.message = filterId; bool result = threadContext.PreTranslateMessage(ref msg); Assert.True(result); mockContext.Verify(c => c.PreFilterMessage(ref It.Ref <Message> .IsAny), Times.Exactly(1)); }
public void ThreadContext_WrongProcessFiltersPassesThrough() { // Test that a filter for the wrong ID returns false, but does get called Application.ThreadContext threadContext = new Application.ThreadContext(); int filterId = TestMessageId2; var mockContext = new Mock <IMessageFilter>(MockBehavior.Strict); mockContext.Setup(c => c.PreFilterMessage(ref It.Ref <Message> .IsAny)) .Returns((MessageCallback)((ref Message m) => m.Msg == filterId)); threadContext.AddMessageFilter(mockContext.Object); NativeMethods.MSG msg = new NativeMethods.MSG(); msg.message = TestMessageId1; bool result = threadContext.PreTranslateMessage(ref msg); Assert.False(result); mockContext.Verify(c => c.PreFilterMessage(ref It.Ref <Message> .IsAny), Times.Exactly(1)); }
public void ThreadContext_CorrectProcessFiltersProcesses() { // Test that a filter with the correct ID returns true Application.ThreadContext threadContext = new Application.ThreadContext(); User32.WindowMessage filterId = TestMessageId2; var mockContext = new Mock <IMessageFilter>(MockBehavior.Strict); mockContext.Setup(c => c.PreFilterMessage(ref It.Ref <Message> .IsAny)) .Returns((MessageCallback)((ref Message m) => m.Msg == (int)filterId)); threadContext.AddMessageFilter(mockContext.Object); var msg = new User32.MSG { message = filterId }; Assert.True(threadContext.PreTranslateMessage(ref msg)); mockContext.Verify(c => c.PreFilterMessage(ref It.Ref <Message> .IsAny), Times.Exactly(1)); }
internal void ForceExitMessageLoop() { IntPtr handle; bool ownHandle; lock (this) { handle = this.handle; ownHandle = this.ownHandle; } if (this.handle != IntPtr.Zero) { if (System.Windows.Forms.UnsafeNativeMethods.IsWindow(new HandleRef(null, this.handle))) { int num; Application.ThreadContext context = Application.ThreadContext.FromId(System.Windows.Forms.SafeNativeMethods.GetWindowThreadProcessId(new HandleRef(null, this.handle), out num)); IntPtr ptr2 = (context == null) ? IntPtr.Zero : context.GetHandle(); if (ptr2 != IntPtr.Zero) { int lpdwExitCode = 0; System.Windows.Forms.SafeNativeMethods.GetExitCodeThread(new HandleRef(null, ptr2), out lpdwExitCode); if (!AppDomain.CurrentDomain.IsFinalizingForUnload() && (lpdwExitCode == 0x103)) { IntPtr ptr3; bool flag1 = System.Windows.Forms.UnsafeNativeMethods.SendMessageTimeout(new HandleRef(null, this.handle), System.Windows.Forms.NativeMethods.WM_UIUNSUBCLASS, IntPtr.Zero, IntPtr.Zero, 2, 100, out ptr3) == IntPtr.Zero; } } } if (this.handle != IntPtr.Zero) { this.ReleaseHandle(true); } } if ((handle != IntPtr.Zero) && ownHandle) { System.Windows.Forms.UnsafeNativeMethods.PostMessage(new HandleRef(this, handle), 0x10, 0, 0); } }