在多个线程运行的COM组件控件多个、线程、控件、组件

2023-09-04 03:06:59 作者:没有星星的夜空

我有一个服务台包我公司,我尝试使用远程桌面集成。在我期待实现的功能是确定(你是在浏览我们的客户名单,同时),如果远程桌面连接是目前可供选择的系统的能力。我使用该库是Microsoft终端服务控制 - (AxInterop.MSTSCLib AxMsRdpClient7)

I have a helpdesk package for our company that I am attempting to integrate with Remote Desktop. Among the functionalities I am looking to implement is the ability to determine (while you are browsing through the list of our clients) if a remote desktop connection is currently available for the selected system. The library I am using is "Microsoft Terminal Services Control" - (AxInterop.MSTSCLib AxMsRdpClient7)

现在我的问题是,我想在一个单独的线程,以prevent阻塞UI(因为我可能会尝试在同一时间上众多客户的连接)执行此任务(尝试连接),从而至今都没有成功。

Now my problem is that I want to perform this task (attempt a connection) on a separate thread to prevent blocking the UI (given that I might be attempting a connection on numerous clients at the same time) and thus far have been unsuccessful.

这里是code一个想法......

Here is an idea of the code...

public void AttemptLogin(string password)
{
    this._thread = new Thread(LoginAttempt);
    this._thread.SetApartmentState(ApartmentState.STA);
    this._thread.Start(password);
}


protected void LoginAttempt(object password)
{
    AxMsRdpClient7 remoteDesktop = new AxMsRdpClient7();
    remoteDesktop.CreateControl();

    remoteDesktop.UserName = this._username;
    remoteDesktop.Server = this._server;
    WireEventHandlers(remoteDesktop);
    IMsTscNonScriptable passwordContainer = (IMsTscNonScriptable)remoteDesktop.GetOcx();
    passwordContainer.ClearTextPassword = password.ToString();
    remoteDesktop.Connect();
}

基本上,code以上的作品完美,如果我执行它在UI线程并添加控件到窗体集合,但是当我试图在一个单独的线程运行这个似乎根本就没有动作发生。无例外提出的连接()。没有引发事件,似乎什么也没有发生。

Basically the code above works perfectly if I am executing it in the UI thread and add the control to the forms collection but when I attempt to run this on a separate thread it appears that simply no actions occur. No exceptions are raised on connect(). No events are raised and it seems nothing happens.

我想我所希望的是确认什么,我试图做(运行一个COM组件在一个线程)为INFACT可能的,什么可能需要得到步骤的任何进一步指导这项工作将是非常AP preciated。

I guess what I am hoping for is confirmation that what I am attempting to do (Run a COM component in a thread) IS INFACT POSSIBLE and any further guidance about what steps might be required to get this to work would be highly appreciated.

推荐答案

好消息是,你想做什么是可能的。由于您所创建的COM对象,并在同一个线程中使用它,那么有没有封送处理问题的担心。 (如果你开始传递COM接口指针到另一个线程,创建该对象将不得不使用一个消息泵STA线程。)

The good news is that what you're trying to do is possible. Since you're creating the COM object and using it in the same thread, then there are no marshalling issues to worry about. (If you start passing COM interface pointers to another thread, the STA thread that created the object would have to use a message pump.)

我不使用MSTSC控制,但我的猜测是,它可能需要被托管在一个窗口前,将工作,即使它是一个隐藏的窗口。我将创建一个新的形式(在你的后台STA线程),看看有没有什么帮助。然后,您可以尝试隐藏的形式,直到你需要显示的终端服务客户端。如果你不确定如何有多种形式在多个线程,请参见多个Windows ,多线程

I've not used the MSTSC control, but my guess is that it may need to be hosted in a window before it will work, even if it's a hidden window. I would create a new form (on your background STA thread) and see if that helps. You can then try hiding the form until you need to display the terminal services client. If you're unsure how to have multiple forms over multiple threads, see Multiple Windows, Multiple Threads