在多个线程上运行 COM 组件控件

发布于 2024-12-01 15:07:37 字数 1129 浏览 0 评论 0原文

我有一个我们公司的帮助台包,我正在尝试将其与远程桌面集成。我希望实现的功能之一是能够确定(当您浏览我们的客户列表时)远程桌面连接当前是否可用于所选系统。我正在使用的库是“Microsoft Terminal Services Control” - (AxInterop.MSTSCLib AxMsRdpClient7)

现在我的问题是我想在单独的线程上执行此任务(尝试连接)以防止阻塞 UI(假设我可能同时尝试连接多个客户端),但到目前为止尚未成功。

这是代码的一个想法...

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();
}

基本上,如果我在 UI 线程中执行上面的代码并将控件添加到表单集合中,那么上面的代码可以完美运行,但是当我尝试在单独的线程上运行它时,似乎根本没有任何操作发生。 connect() 上不会引发异常。没有引发任何事件,似乎什么也没有发生。

我想我所希望的是确认我正在尝试做的事情(在线程中运行 COM 组件)实际上是可能的,并且任何有关使其正常工作可能需要哪些步骤的进一步指导将受到高度赞赏。

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)

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.

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();
}

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆凄美了谁 2024-12-08 15:07:37

好消息是您尝试做的事情是可能的。由于您正在创建 COM 对象并在同一线程中使用它,因此无需担心编组问题。 (如果您开始将 COM 接口指针传递给另一个线程,则创建该对象的 STA 线程将必须使用消息泵。)

我没有使用 MSTSC 控件,但我的猜测是它可能需要托管在窗口才能工作,即使它是一个隐藏窗口。我会创建一个新表单(在您的后台 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.)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文