开发者

Running COM component controls on multiple threads

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;
    WireEventHandler开发者_如何学JAVAs(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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜