首页 > 解决方案 > Communicate between process in C#

问题描述

I have a service that calls a process to create a file (the process is a winform), In my service, I want to read the file once the process creates the file, how can I communicate between my service and the process to check that the file has been created correctlly.

Process.Start(assemblyPath, xsdFileName + " " + fileName + " " + exportPDFFileName);
//here I need to make sure that the file has been created correctly
return new MemoryStream(File.ReadAllBytes(exportPDFFileName));

Thank you for your help, Bilel

标签: c#

解决方案


A simple solution is using process.WaitForExit() to know when the process ended:

Process.Start(assemblyPath, xsdFileName + " " + fileName + " " + exportPDFFileName);
process.WaitForExit();
return new MemoryStream(File.ReadAllBytes(exportPDFFileName));

Note this will work only if you know for sure that the process actually creates the file. Otherwise, I suggest either looping with a sleep to make sure the file was created (using File.Exists(path)), or reading the process output to make sure it created the file. Read this post for this option.


推荐阅读