CLOSE 事件后如何保存和删除文档? [英] How to save And delete document after CLOSE event?
问题描述
我有一个将word文档存储在数据库中的程序,它们被用户反复打开、关闭,然后保存回数据库.打开时我将它们放在临时文件夹中.但是关闭的时候想保存回数据库然后直接删除.
I have a program that stores word documents in a database, and they are repeatedly opened, closed by the user and then saved back to the database. when opening I place them on a temp folder. but when closed, I want to save is back to the database and then delete it directly.
我试过了:
...
((DocumentEvents_Event)document).Close += DocumentClose;
...
delegate void MethodDelegate();
private void DocumentClose()
{
new MethodDelegate(deleteLater)();
}
void deleteLater()
{
//document.Close();
Thread.Sleep(1000);
File.Delete(this.tempDocFilePath);
}
但这不起作用,我收到一条错误消息,告诉我文件已打开.当我取消注释document.Close();"时接下来的两行不执行
but this don't work, and I get an error message telling me the file is already opened. and when I uncomment "document.Close();" the next two lines are not excuted
有什么想法吗?
推荐答案
此代码可能存在竞争条件.永远不要相信睡眠解决方案.等待特定事件或民意调查,然后采取行动.
This code is possibly subject to a race condition. NEVER trust a Sleep-solution. Wait for a specific event or poll and then take action.
这篇关于CLOSE 事件后如何保存和删除文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!