打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Call to OpenFileDialog or SaveFileDialog hangs or freezes | WishMesh

Today I was debugging a very strange issue when call to OpenFileDialog and SaveFileDialog hanged, froze, never returned…

At first I thought the problem is, that call is made from SysTray (Notification area) hidden window. However, digging deeper things got more bizarre. I found a thread in MS forum, where was a workaround – add help button to OpenFileDialog.

When I say that it “hangs” I mean that the dialog does not display but no exception is thrown and the system eventually throws in a little wait cursor to indicate that the program is getting time. It doesn’t hang the entire computer, fortunately, but the application has to be terminated.

For OpenFileDialog the ShowHelp property must be explicitly set.
For SaveFileDialog the ShowHelp, CreatePrompt, and OverwritePrompt properties must be explicitly set.

It’s not consistent though. I’ve had SaveFileDialog work with just ShowHelp set in simple situations. But I have an app which didn’t start running again until I also set CreatePrompt and OverwritePrompt properties then it started working again. They don’t have to be set to true, they just have to be initialized to either true or false.

Original forum post: click here.

So I tried:
myDialog.ShowHelp = true;

And it worked. So bizarre I thought.

After little more testing I found next problem. If file is not found, then dialog must show warning that ‘File not found. Check the file name and try again.’ And it showed the warning… and hanged…

From the beginning I was ignoring one fact, that this actually is a .NET COM object, and that I am calling it from MTA threading model (multi-thread apartment). By default .NET COM objects advertise themselves as BOTH (STA, MTA), so I changed calling thread to STA. Changed CoInitializeEx to CoInitialize, and suddenly everything worked like a charm.

I am not a COM expert, so if you find some flaws please let me know in comments!

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
CommonDialog 控件(针对 Visual Basic 6.0 用户)
DevExpress GridControl 部分用法
C#中打开文件、目录、保存窗口
[OpenCV+C#]开发环境的搭建
Vladimir Prus: Spurious wakeups
c_对话框大全
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服