2

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.

2
  • You might want to read this question I had: stackoverflow.com/questions/804968/…. In my case, I create and run the ActiveX control in a timer thread. Commented Aug 26, 2011 at 0:28
  • You are violating a hard requirement for STA threads: they must pump a message loop. It is used for marshaling calls but COM components often use it internally as well to take care of synchronization. Commented Aug 28, 2011 at 16:04

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Sucks that hosting it in a window is required but seems to be the fix... Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.