C# - How to pause the program until an image file is done being overwritten -


in program, there process goes this:

save image (overwrite existing one) -> run ocr on image -> return string result

this process can repeated several times in row, , without thread.sleep(), results in:

1st iteration: save img01 -> run ocr on img01 -> return result of img01

2nd iteration: save img02 replace img01 -> still run ocr on img01 (old img) -> return result of img01 (old result)

(img01 , img02 share same file name/path)


because there no pause in flow, notice ocr take place on img01 instead of img02 because img02 still being saved.

the unreliable way current pc work put down thread.sleep(1000) between saving image , running ocr. can imagine not right solution since different computer take different times save image right?

how can make program pause dynamically , wait image overwritten before continues next step?

one way this:

first should check if img02 exists , if file curently in use.

in order check if img02 exists like: file.exists(path)

then check if file in use, like:

protected virtual bool isfilelocked(fileinfo file) {     filestream stream = null;      try     {         stream = file.open(filemode.open, fileaccess.read, fileshare.none);     }     catch (ioexception)     {         //the file unavailable because is:         //still being written         //or being processed thread         //or not exist (has been processed)         return true;     }         {         if (stream != null)             stream.close();     }      //file not locked     return false; } 

if file not in use can proceed next step.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -