简体   繁体   中英

Can I filter message from a window in a different thread?

I'm writing an application that uses TwainDotNet for scanning.

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.

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.

To create a thread that runs a message loop use 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. However, like I said, you may not have choice. Also, keep all activity occurring on these two UI threads completely separate. You should not attempt to access UI controls running on your main UI thread from this secondary UI thread. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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