2013-08-12 7 views
6

एक डब्ल्यूपीएफ एप्लीकेशन के लिए, क्या आंतरिक रूप से क्लासिक संदेश लूप (विंडोज़ GetMessage/DispatchMessage भावना में) Application.Run के अंदर है? क्या किसी अन्य Win32 एप्लिकेशन से PostThreadMessage के साथ एक WPF UI थ्रेड (HWND हैंडल के बिना एक संदेश) में पोस्ट किए गए संदेश को पकड़ना संभव है। धन्यवाद।डब्ल्यूपीएफ एप्लीकेशन मैसेज लूप और पोस्ट थ्रेड मैसेज

+2

[घटक डिस्पैटर। थ्रेडफिल्टर मैसेज] (http://msdn.microsoft.com/en-us/library/system.windows.interop.componentdispatcher.threadfiltermessage.aspx) ईवेंट के साथ एक विशिष्ट संदेश के लिए देखना संभव हो सकता है, हालांकि दस्तावेज़ कहते हैं कि यह कीबोर्ड संदेशों के लिए है। यहां एक संबंधित प्रश्न है [उत्तर] (http://stackoverflow.com/questions/476084/c-sharp-twain-interaction)। – Noseratio

उत्तर

3

मैंने Applicaton.Run को Dispatcher.PushFrameImpl पर कार्यान्वित करने के लिए .NET परावर्तक का उपयोग किया। .NET Framework reference sources से एक ही जानकारी प्राप्त करना भी संभव है।

private void PushFrameImpl(DispatcherFrame frame) 
{ 
    SynchronizationContext syncContext = null; 
    SynchronizationContext current = null; 
    MSG msg = new MSG(); 
    this._frameDepth++; 
    try 
    { 
     current = SynchronizationContext.Current; 
     syncContext = new DispatcherSynchronizationContext(this); 
     SynchronizationContext.SetSynchronizationContext(syncContext); 
     try 
     { 
      while (frame.Continue) 
      { 
       if (!this.GetMessage(ref msg, IntPtr.Zero, 0, 0)) 
       { 
        break; 
       } 
       this.TranslateAndDispatchMessage(ref msg); 
      } 
      if ((this._frameDepth == 1) && this._hasShutdownStarted) 
      { 
       this.ShutdownImpl(); 
      } 
     } 
     finally 
     { 
      SynchronizationContext.SetSynchronizationContext(current); 
     } 
    } 
    finally 
    { 
     this._frameDepth--; 
     if (this._frameDepth == 0) 
     { 
      this._exitAllFrames = false; 
     } 
    } 
} 

इसके अलावा, यहां TranslateAndDispatchMessage के कार्यान्वयन, जो वास्तव में RaiseThreadMessage अंदर निष्पादन के अपने पाठ्यक्रम के साथ ComponentDispatcher.ThreadFilterMessage ईवेंट सक्रिय है:: वहाँ वास्तव में एक क्लासिक संदेश पाश है

private void TranslateAndDispatchMessage(ref MSG msg) 
{ 
    if (!ComponentDispatcher.RaiseThreadMessage(ref msg)) 
    { 
     UnsafeNativeMethods.TranslateMessage(ref msg); 
     UnsafeNativeMethods.DispatchMessage(ref msg); 
    } 
} 

जाहिर है, यह किसी भी पोस्ट के लिए काम करता है संदेश, सिर्फ कीबोर्ड वाले नहीं। आपको ComponentDispatcher.ThreadFilterMessage पर सब्सक्राइब करने और रुचि के संदेश के लिए देखने में सक्षम होना चाहिए।

संबंधित मुद्दे