そのため、windowsAPICodepack の CommonOpenFileDialog を使用しています。私が書いているアプリケーションの以前のバージョンでは、CommonOpenFileDialog は問題なく動作しました。上位バージョンの .Net Framework を対象とする現在のバージョンでは、メイン フォームから toolstripMenuItem クリック イベントを介してメイン UI スレッドからダイアログが呼び出されても、クロススレッド操作が有効でないという例外が発生します。以前は、メイン フォームのボタン クリック ハンドラから同様の方法で呼び出されていました。
同じフォームで CommonOpenFileDialog を表示する同じコードを明示的に呼び出すと、これは解決しますが、通常のダイアログの動作はこの方法では失われます。
これは機能しますが、 this.Invoke がないと機能しません。
private void loadWorkspaceToolStripMenuItem_Click(object sender, EventArgs e)
{
bool wait = true;
this.Invoke((MethodInvoker)delegate ()
{
string startPath = LastUsedPath;
if (FileFolderTools.ShowFolderChooser(ref startPath))
{
workspace.LoadWorkspace(startPath);
LastUsedPath = startPath;
}
wait = false;
});
while(wait){}
}
さらに、while(待機) はありますが、UI は応答しますが、通常、ボタンを押してブロッキング呼び出しが行われた場合はそうではありません。ここで何が起こっているのかわからない...
編集:下部にあるより広範なスタックトレース。
this.Invoke が呼び出されたときのコール スタック:
編集 - これが ShowfolderChooser の動作です (OK の場合は true、キャンセルの場合は false を返します)。
public static bool ShowFolderChooser(ref string path){
CommonOpenFileDialog dialog = new CommonOpenFileDialog();
dialog.InitialDirectory = path;
dialog.IsFolderPicker = true;
CommonFileDialogResult res = dialog.ShowDialog();
if (res == CommonFileDialogResult.Ok)
{
path = dialog.FileName; //set new path on OK
return true;
}
return false;
}
完全な例外:
This exception was originally thrown at this call stack:
[External Code]
DataManagement.DataManagementCore.Helpers.FileFolderTools.ShowFolderChooser(ref string) in FileFolderTools.cs
Camera._Camera.btn_exportAll_Click(object, System.EventArgs) in Camera.cs
[External Code]
Camera.Program.Main() in Program.cs
ちょっと待って...!もっとある...
そのため、そのコードを別のアプリケーションに入れてみましたが、問題なく動作します。だから私はそれが私のアプリケーションで何かだと思います。大きな違いの 1 つは、上のスタック フレームにloadWorkspaceToolStripMenuItem_Click
スタック エントリuser32.dll![Frames...
があることです。それと何か関係があると思います...
EDIT2より広範なスタックトレース:
at System.Windows.Forms.Control.get_Handle()
at Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialog.ApplyNativeSettings(IFileDialog dialog)
at Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialog.ShowDialog()
at Camera._Camera.btn_exportAll_Click(Object sender, EventArgs e) in D:\XXXXXXXXXXXXXX\TestProjects\DataManagementTestProject\Camera\Mainscreen\Camera.cs:line 661
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at DataManagementTestProject.Program.Main() in D:\XXXXXXXXXXXXXX\TestProjects\DataManagementTestProject\Program.cs:line 20
このスタック トレースから、UI のメイン スレッドで呼び出しが行われたことを確認できます。どこかでうまくいかない getHandle に問題があるようです。しかし、これは動作するはずのAPIパックに含まれています...