dll中 通过事件发消息,怎么回事,求助,

lulu23260 2014-03-05 07:20:54
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类中的注释部分,调用方是可以收到消息的,
这里错误是???或者有更合理的方法???
...全文
155 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
lulu23260 2014-03-05
  • 打赏
  • 举报
回复
引用 1 楼 caozhy 的回复:
试试看SendMessage
刚试了,调用方能收到消息,但执行一次之后,还是卡住了,无响应…… 能不能帮我看看,我这么做合理吗?有好的方法没,这实属自己想当然的写出路的思路
threenewbee 2014-03-05
  • 打赏
  • 举报
回复
试试看SendMessage

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧