Last active
August 29, 2015 14:06
-
-
Save adilsoncarvalho/5b978e2d55985d91adf7 to your computer and use it in GitHub Desktop.
Showing Dialogs in STA (very useful when dealing with SAP forms)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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