我看了this帖子,在我的代码中,一切似乎都是正确的!
我使用Dockpanel套件来保存&负载布局。问题在于保存布局。从visual studio调试时,当我关闭winform(保存布局)时需要很长时间(10秒),然后VS停止调试会话。
如果我在关闭申请后直接运行exe(需要点击' x'按钮两次!)我收到以下错误
有关调用实时的详细信息,请参阅此消息的结尾 (JIT)调试而不是此对话框。
**************异常文本**************
System.IO.IOException:进程无法访问该文件 ' C:\ Users \ Admin \ Documents \ Visual Studio 2015 \ Projects \ test \ Release \ Layout.config'因为它被另一个人使用 处理。
在System.IO .__ Error.WinIOError(Int32 errorCode,String maybeFullPath)
在System.IO.FileStream.Init(String path,FileMode 模式,FileAccess访问,Int32权限,布尔useRights,FileShare share,Int32 bufferSize,FileOptions选项,SECURITY_ATTRIBUTES secAttrs,String msgPath,Boolean bFromProxy,Boolean useLongPath, Boolean checkHost)
在System.IO.FileStream..ctor(String path, FileMode模式,FileAccess访问,FileShare共享,Int32 bufferSize, FileOptions选项,String msgPath,Boolean bFromProxy)
at System.IO.FileStream..ctor(String path,FileMode mode)
在 InsightRealityv2.Main.Main_FormClosing(对象发送者, FormClosingEventArgs e)在c:\ users \ Admin \ documents \ visual studio中 2015 \ projects \ test \ insight现实v2 \ main.h:第573行 。
。
现在,根据错误消息" Layout.config"仍然是开放的。但我正在关闭FileStream。下面是我的代码(表单加载时调用的函数)
Void load_layout()
{
System::String^ path = Path::Combine(Path::GetDirectoryName(Application::ExecutablePath), "Layout.config");
if (File::Exists(path))
{
FileStream ^fs1 = gcnew FileStream(path, FileMode::Open, FileAccess::Read);
Stream ^s1 = fs1;
DeserializeDockContent ^m_deserializeDockContent = gcnew DeserializeDockContent(this, &Main::GetContentFromPersistString);
dockPanel1->LoadFromXml(path, m_deserializeDockContent);
fs1->Close();
s1->Close();
}
}
当表单结束事件触发时,将调用以下代码
Void Save_Layout()
{
System::String^ path = Path::Combine(Path::GetDirectoryName(Application::ExecutablePath), "Layout.config");
FileStream ^fs = gcnew FileStream(path, FileMode::Create);
Stream ^s = fs;
dockPanel1->SaveAsXml(s, System::Text::Encoding::Unicode, false);
fs->Close();
s->Close();
}
该函数仅从同一UI线程调用一次。