Skip to content

Instantly share code, notes, and snippets.

@adilsoncarvalho
Last active August 29, 2015 14:06
Show Gist options
  • Save adilsoncarvalho/5b978e2d55985d91adf7 to your computer and use it in GitHub Desktop.
Save adilsoncarvalho/5b978e2d55985d91adf7 to your computer and use it in GitHub Desktop.
Showing Dialogs in STA (very useful when dealing with SAP forms)
public static class FileDialogExtension
{
internal class SapMainWindow : IWin32Window
{
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
public IntPtr Handle { get { return GetForegroundWindow(); } }
}
internal class DialogState
{
public DialogResult Result;
public FileDialog Dialog;
public void ThreadProcShowDialog()
{
Result = Dialog.ShowDialog(new SapMainWindow());
}
}
// ReSharper disable once InconsistentNaming
public static DialogResult ShowDialogSTA(this FileDialog dialog)
{
var state = new DialogState { Dialog = dialog };
var t = new System.Threading.Thread(state.ThreadProcShowDialog);
t.SetApartmentState(System.Threading.ApartmentState.STA);
t.Start();
t.Join();
return state.Result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment