c# activex控件 在其他线程中触发事件问题
我做了一个activex控件,是用c#写的,在网页注册activex控件,和传参等操作都没有问题。在主线程中触发事件也没有什么问题。关键是,当我在线程中触发事件时,页面就接收不到了,想问一下,这个问题怎么解决。我在网上找到了c++的解决方法,但因我不太懂c++.所以希望高手能给个c#的解决方案。我的代码如下:
一。用户控件定义如下
//一定要引入下面的这句,否则无法使用自定义的事件。
//[ComSourceInterfacesAttribute(typeof(InterviewActiveBridge.IClientEvent))]
[Guid("ea278f34-6048-4671-9480-12fff66a2194")]
[
ClassInterface(ClassInterfaceType.AutoDual),
ComSourceInterfaces(typeof(IClientEvent)),
ComVisible(true)
]
public partial class UserControlBridge : UserControl, IObjectSafety
{
public UserControlBridge()
{
InitializeComponent();
}
public delegate void Message(string message);
public event Message MessageEvent;
public void GetInterfacceSafyOptions(System.Int32 riid, out System.Int32 pdwSupportedOptions, out System.Int32 pdwEnabledOptions)
{
pdwSupportedOptions = 1; //不要修改该代码
pdwEnabledOptions = 2; //不要修改该代码
return;
}
public void SetInterfaceSafetyOptions(System.Int32 riid, System.Int32 dwOptionsSetMask, System.Int32 dwEnabledOptions)
{
return;
}
public string GetMessage()
{
if (MessageEvent != null)
{
MessageEvent.Invoke(message);
}
}
public void ShowThreadEvent()
{
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(this.ShowEvent));
thread.IsBackground = true;
thread.Start();
}
public void ShowEvent()
{
MessageBox.Show("线程:" + System.Threading.Thread.CurrentThread.Name + ";ID" + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
if (MessageEvent != null)
{
MessageEvent.Invoke("我是被动触发的");
}
}
}
二.实现了下面的事件接口
//注意该接口,若要在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访问就会出问题。 --------------------编程问答-------------------- --------------------编程问答-------------------- 没有人能帮助解决一下吗? --------------------编程问答-------------------- 学习中!!! --------------------编程问答-------------------- 自已顶 --------------------编程问答-------------------- 同样问题。 顶 --------------------编程问答-------------------- 问题很高深,我只是个菜鸟,学习中……
补充:.NET技术 , C#