I have this:
Dim myTemp As String
myTemp = System.DateTime.Now().ToString("MMMddyyyy_HHmmss") & ".pdf"
System.IO.File.Copy(myFile, "c:\" & myTemp)
Application.DoEvents()
OpenFile(myTemp)
The problem is that when I call OpenFile, which is just a call to a sub that opens a file, it cannot find the file. This is because it is calling it so quickly that the program doesn't have time to actually create the file before the open takes place.
I thought that DoEvents() would rectify this but it does not. I need to wait until the file is created before I open the file. How can I do that?
First, you should not call DoEvents anywhere. For the most part, when it is used, it is a hack to circumvent what should really be an asynchronous operation.
That being said, the
Copy
method is a synchronous operation. The call toOpenFile
will not occur until the call toCopy
completes.That being said when the call to
OpenFile
occurs, if the file does not exist, it is because you copied it to the wrong place, or because some other process is working on the file in question.This is a bit hacky, but it should work.
In addition to Tom's answer, isnt it better to put a Application.DoEvents() rather then making the thread sleep?
I thinf Synclock is not good for this case
for explaination, MSDN can help me
in my opinion, copy is blocking method, so thread waits until copying is done
can`t be problem in another place?
System.Threading.Thread.Sleep(1000);