I have a simple piece of code that puts a file object into the clipboard, starts a process, and then pastes the file object into the external application to process it. The problem I have is that the code doesn't run unless I insert a Process.WaitForExit after the paste code. I want to close the process programmatically when the output file is created, but I can only do this manually due to the Process.WaitForExit line. If I don't have the Process.WaitForExit, then the external application resizes and repositions correctly, and then does not accept the dropped file, which has disappeared from the filedrop list.
Dim dataObject As New DataObject
Dim dropFileName As String() = New String(0) {}
dropFileName(0) = ListBox1.Items.Item(0)
dataObject.SetData(DataFormats.FileDrop, True, dropFileName)
Clipboard.SetDataObject(dataObject)
Process2.Start()
Process2.PriorityBoostEnabled = True
Process2.WaitForInputIdle()
Thread.Sleep(2000) ' the external app is slow to accept file after start
Dim appTitle As String = "PDF OCR X"
Dim appHandle As IntPtr = FindWindowByCaption(0, appTitle)
SetWindowPos(appHandle, -1, 0, 0, 150, 200, &H40) 'set up the external window...
'all this ^ works fine
SendKeys.SendWait("{F10}") 'open the menu
SendKeys.SendWait("{RIGHT}") 'got to edit menu
SendKeys.SendWait("{DOWN}") 'go to 'Paste' option
SendKeys.SendWait("{ENTER}") 'fire 'Paste' option
'this ^ only works with the WaitForExit!
Process2.WaitForExit()
'owing to WaitForExit the external app has to be closed manually to execute the rest
sleepTime = 0
Do While File.Exists(outputOCRFullPath) = False
Thread.Sleep(500) 'OCR is slow!
sleepTime += 0.5
Loop
Try
File.Move(outputOCRFullPath, "K:\" & targetDirectory & "\" & inputFileName.Replace(".pdf", ".txt"))
Catch ex As Exception
MsgBox("Wur doomed! Sleeptime = " & sleepTime)
End Try
I am trying to understand this behaviour, in order to implement a solution, so any advice would be much appreciated.
Ta everso!