dll中 通过事件发消息,怎么回事,求助,
dll类库中代码:MyUnion_1是自定义的一个类似union的struct,,想要实现将费时的操作myFunction2的进行的状态,切换的时候触发事件,事件注册方法jinggao,即向调用方窗口发送消息,消息参数是费时函数的状态,
namespace test{
public class dele
{
public delegate void luluDelegate(IntPtr hwd,string str,MyUnion_1 mu);
public event luluDelegate luluEvent;
public void myFunction2(IntPtr hwd, string str) //耗时的操作主体部分
{
string temp = str;
MyUnion_1 mu;
mu.nret=0x2222;
mu.u2.errorCode=0x81;
mu.u2.cmd=0x82;
luluEvent(hwd,str, mu);
for (;temp.Length < 600;)
{
temp.PadLeft(temp.Length+1,'0');
Thread.Sleep(1000);
}
mu.nret = 0x3333;
luluEvent(hwd,str, mu);
}
[DllImport("User32.dll", EntryPoint = "PostMessage")]
public static extern int PostMessage(IntPtr hwnd, int msg, MyUnion_1 wpara, int lpara);
public void jinggao(IntPtr hwd, string str, MyUnion_1 mu) //可以发通知的函
{
Console.WriteLine("333--进入事件绑定函数 {0} {1}", hwd,mu);
PostMessage(hwd, WM_CMD_EXECUTE_RESULT, mu, 0);
Console.WriteLine("333--完成postmessage ");
}
}
public class Class1
{
const int WM_CMD_EXECUTE_RESULT2 = /*WM_USER + */0x66;
//[DllImport("User32.dll", EntryPoint = "PostMessage")]
//public static extern int PostMessage(IntPtr hwnd, int msg, int wpara, int lpara);
public void my_test(IntPtr hwd, string str)
{
//PostMessage(hwd, WM_CMD_EXECUTE_RESULT2, 0, 0);
dele my_dele = new dele();
my_dele.luluEvent += my_dele.jinggao; //注册方法
my_dele.myFunction2(hwd,"XiaoHuaHua");
}
}
}
调用方windows程序,改写消息处理函数,调用Class1.my_test之后,调用方没有收到消息,并且卡住无响应,
但若是,打开Class1类中的注释部分,调用方是可以收到消息的,
这里错误是???或者有更合理的方法???