c# - 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
解决方案
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.
推荐阅读
- java - 应用 android.useAndroidX=true 和 android.enableJetifier=true 来修复错误会创建另一个错误
- sql - 在这种情况下,我们如何在 sql 中使用数据透视?
- javascript - 我可以自定义 Twilio 可编程视频中参与者共享的接收视频的大小吗?
- jgit - 有没有办法从特定的 DiffEntry 或 RevCommit 获取子模块信息?
- php - BadMethodCallException 调用未定义的方法 App\Post::tags()
- flutter - 导航器弹出当前页面时颤动中的Renderflex错误?
- php - 跨域请求被阻止:同源策略不允许读取远程资源不起作用
- python - 如何使用现有数据框的具体信息生成新数据框
- selenium - 使用 scrapy 加载会话 cookie
- python-3.x - 如何在两个 numpy 张量之间进行 numpy matmul 广播?