简体   繁体   English

我可以从不同线程中的窗口过滤消息吗?

[英]Can I filter message from a window in a different thread?

I'm writing an application that uses TwainDotNet for scanning. 我正在编写一个使用TwainDotNet进行扫描的应用程序。

Everything works fine, but scanning makes the user interface unusable. 一切正常,但扫描使用户界面无法使用。 So I decided to scan in a different thread, but I never got the scanning successful event. 所以我决定扫描一个不同的线程,但我从来没有得到扫描成功的事件。 So I decided to take a closer look at the implementation: 所以我决定仔细看看实现:

public DataSourceManager(Identity applicationId, 
    IWindowsMessageHook messageHook)
{
    // Make a copy of the identity in case it gets modified
    ApplicationId = applicationId.Clone();

    ScanningComplete += delegate { };
    TransferImage += delegate { };

    _messageHook = messageHook;
    _messageHook.FilterMessageCallback = FilterMessage;
    IntPtr windowHandle = _messageHook.WindowHandle;

    _eventMessage.EventPtr =  
           Marshal.AllocHGlobal(Marshal.SizeOf(typeof(WindowsMessage)));

And the method that does the filtering: 以及进行过滤的方法:

protected IntPtr FilterMessage(IntPtr hwnd, int msg, IntPtr wParam, 
    IntPtr lParam, ref bool handled)
{

Normally this method filters a log of messages, but if I scan in a different thread I don't receive any . 通常,此方法会过滤消息日志,但如果我在另一个线程中扫描,则不会收到任何消息。

The scanning part of my code is: 我的代码的扫描部分是:

    var scanningThread = new Thread((ThreadStart) delegate
    {
        // Previously I got the main UI form here, but that didn't work either
        var form = new Form();
        var messageHook = new WinFormsWindowMessageHook(form.Handle);

        var scanner = new TwainEngine(messageHook);
        scanner.TransferImage += TransferImage;
        scanner.ScanningComplete += ScanningComplete;
        scanner.StartScanning(twainSettings);

    });
    scanningThread.Start();

I know nothing about the Win32 message architecture so any input on how I could solve this problem or what causes this problem would be helpful. 我对Win32消息架构一无所知,所以关于如何解决这个问题或者导致这个问题的原因的任何输入都会有所帮助。

The code you have will not work correctly because you are creating a form on a thread without a message loop. 您拥有的代码将无法正常工作,因为您在没有消息循环的线程上创建表单。 If this library really does require a form or message loop and it is hanging up the UI then aside from it having design issues this may be one of the very rare scenarios where you might want to consider having a second thread running a message loop. 如果这个库确实需要一个表单或消息循环并且它挂起了UI,那么除了它有设计问题之外,这可能是你可能想要考虑让第二个线程运行消息循环的非常罕见的场景之一。

To create a thread that runs a message loop use Application.Run . 要创建运行消息循环的线程,请使用Application.Run Your modified code might look like this. 您修改后的代码可能如下所示。

var scanningThread = new Thread((ThreadStart) delegate
{
    var form = new Form();
    form.Load += (sender, args) =>
    {
      var messageHook = new WinFormsWindowMessageHook(form.Handle);
      var scanner = new TwainEngine(messageHook);
      scanner.TransferImage += TransferImage;
      scanner.ScanningComplete += ScanningComplete;
      scanner.StartScanning(twainSettings);
    };   
    Application.Run(form);
});
scanningThread.Start();

I must point out that having more than one UI thread can cause some weird problems so it is not generally advised. 我必须指出,拥有多个UI线程可能会导致一些奇怪的问题,因此通常不建议这样做。 However, like I said, you may not have choice. 但是,就像我说的那样,你可能没有选择。 Also, keep all activity occurring on these two UI threads completely separate. 此外,将这两个UI线程上发生的所有活动完全分开。 You should not attempt to access UI controls running on your main UI thread from this secondary UI thread. 您不应尝试从此辅助UI线程访问在主UI线程上运行的UI控件。 There are all sorts unpredictable and spectacular problems that will occur. 会发生各种不可预测和引人注目的问题。 If you need to force an action to occur on one thread use Invoke or BeginInvoke to marshal the execution of a delegate. 如果需要强制在一个线程上执行操作,请使用InvokeBeginInvoke来编组委托的执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM