Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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!
Posted

1 solution

Ok, I solved the problem - though I still don't understand why it worked the way it did!

The weird thing is that PDF OCR X uses the launched executable to create an empty file with .pdf replaced by .txt and saves it in the default directory. Then, it closes the file and hands it over to a java executable, which then opens file to save the ocr output - and becomes the process that was first opened. Process.close and process.kill did nothing!

I couldn't find a solution, so I built a workaround - I simply set up a new thread to monitor the file being locked, leaving the Process.WaitForExit in place, and as soon as the file was unlocked I moved the file and used user32.dll to get the hWnd and then sendmessage to close the app from the new thread.

Job done!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900