I'm writing a connection handler (a dialog to request username and password). The code is a handler that shows a dialog. This code could be called from a thread, so I need to Invoke()
if InvokeRequired
.
In a ideal situation I can initialize then handler with a Control
to do InvokeRequired
, but sometimes the Control could be null. Is it possible? How could I implement the code? Is the following correct?
public class GuiCredentialsHandler
{
// control used to invoke if needed
private static Control mInvokeControl;
/// <summary>
/// Initialize a GetCredentials handler for current process.
/// This method should be always called from the main thread, for
/// a correctly handling for invokes (when the handler is called
/// from a thread).
/// </summary>
/// <param name="parentControl">Application top form.
/// Can be null if unknown</param>
public static void Initialize(Control parentControl)
{
if (parentControl != null)
{
mInvokeControl = parentControl;
}
else
{
mInvokeControl = new Control();
// force to create window handle
mInvokeControl.CreateControl();
}
}
public static Credentials GetCredentials()
{
if (mInvokeControl.InvokeRequired)
{
return mInvokeControl.Invoke(
new GetCredentialsDelegate(DoGetCredentials), null)
as Credentials;
}
else
{
return DoGetCredentials();
}
}
private static Credentials DoGetCredentials()
{
// the code stuff goes here
}
}
My questions are:
- What happens if I pass a null control to the
InitializeMethod()
- If the Initialize() method is executed in the UIThread, will the code work later?
- What is the recommended pattern if you havn't got any control to test
InvokeRequired
?
Thanks in advance
EDIT: Doing some tests, I have realized that if I pass null to Initialize()
, the control is not running in the UI thread so the InvokeRequired seems to return false. Always. So my question is, how can I perform a real (fiable) Invoke when I have no control?
EDIT2: Doing mInvokeControl.CreateControl()
fixs the issue.