user32.dll SendMessage命令使消息发送应用程序暂停执行

时间:2012-08-15 08:52:22

标签: c# dllimport user32

我正在使用user32 SendMessage dll命令将命令传输到Windows应用程序。

[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

其中一个命令会导致应用程序显示接受输入的模式框。

我的问题是:为什么我的应用程序的代码执行会停止,直到另一个应用程序的模式框被关闭?

有没有办法继续执行我的应用程序代码而不会受到使用user32.dll发送邮件时所引起的暂停的干扰?

2 个答案:

答案 0 :(得分:4)

SendMessage将阻止,直到呼叫接收者完成处理消息。

您可以使用PostMessage,这将允许您的程序在分派邮件后立即继续执行。

答案 1 :(得分:1)

您可以阅读有关线程here

的信息

这是一个简单的例子:

using System.Threading;

public static void DoSendMessage() 
{
    SendMessage(...); 
}

public void RunSendMessage()
{
  ThreadStart threadDelegate = new ThreadStart(DoSendMessage);
  Thread newThread = new Thread(threadDelegate);
  newThread.Start();
}
相关问题