首页 新闻 会员 周边

c# activex 在其他线程中触发事件问题

0
悬赏园豆:160 [待解决问题]

我做了一个activex控件,是用c#写的,在网页注册activex控件,和传参等操作都没有问题。在主线程中触发事件也没有什么问题。关键是,当我在线程中触发事件时,页面就接收不到了,想问一下,这个问题怎么解决。我在网上找到了c++的解决方法,但因我不太懂c++.所以希望高手能给个c#的解决方案。

我的代码如下:

一。用户控件定义如下

 

1
2 //一定要引入下面的这句,否则无法使用自定义的事件。
3 //[ComSourceInterfacesAttribute(typeof(InterviewActiveBridge.IClientEvent))]
4   [Guid("ea278f34-6048-4671-9480-12fff66a2194")]
5 [
6 ClassInterface(ClassInterfaceType.AutoDual),
7 ComSourceInterfaces(typeof(IClientEvent)),
8 ComVisible(true)
9 ]
10 public partial class UserControlBridge : UserControl, IObjectSafety
11 {
12
13 public UserControlBridge()
14 {
15 InitializeComponent();
16 }
17
18
19 public delegate void Message(string message);
20 public event Message MessageEvent;
21
22
23
24
25 public void GetInterfacceSafyOptions(System.Int32 riid, out System.Int32 pdwSupportedOptions, out System.Int32 pdwEnabledOptions)
26 {
27 pdwSupportedOptions = 1; //不要修改该代码
28 pdwEnabledOptions = 2; //不要修改该代码
29 return;
30 }
31 public void SetInterfaceSafetyOptions(System.Int32 riid, System.Int32 dwOptionsSetMask, System.Int32 dwEnabledOptions)
32 {
33 return;
34 }
35
36 public string GetMessage()
37 {
38 if (MessageEvent != null)
39 {
40 MessageEvent.Invoke(message);
41 }
42
43 }
44 public void ShowThreadEvent()
45 {
46 System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(this.ShowEvent));
47 thread.IsBackground = true;
48 thread.Start();
49 }
50 public void ShowEvent()
51 {
52 MessageBox.Show("线程:" + System.Threading.Thread.CurrentThread.Name + ";ID" + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
53 if (MessageEvent != null)
54 {
55 MessageEvent.Invoke("我是被动触发的");
56 }
57 }
58
59 }
60

二.实现了下面的事件接口

//注意该接口,若要在ie中可以使用自定义的事件,必须要实现了该接口。
[
Guid(
"20540893-D8E7-4824-8DF4-8681D2380C19"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch),
ComVisible(
true)
]
public interface IClientEvent
{
[DispId(
0)]
void MessageEvent(string msg);
}

在ie中注册后,直接调用方法GetMessage()触发事件没有问题。
但是如果在线程中触发事件则有问题,而且问题是,在本机上没有问题,可以正常触发。部署到iis上,在没有调试用该源码的电脑上使用ie访问就会出问题。

问题补充: 高手能否给个解决方法。我的分都给了,希望得到一个解决方案。
joery的主页 joery | 初学一级 | 园豆:40
提问于:2010-09-08 08:37
< >
分享
所有回答(1)
0

你问题解决了没

kanek | 园豆:206 (菜鸟二级) | 2017-03-08 14:17
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册