I have process that i am opening and create file on dist and subscribe to this procees ProcessExitedEvent
:
public int InvokeProcess(WiresharkProcesses process, string args)
{
try
{
string processToInvoke = null;
ProcessStartInfo startInfo = new ProcessStartInfo();
Process pros = new Process();
startInfo .FileName = processToInvoke;
startInfo .RedirectStandardOutput = true;
startInfo .RedirectStandardError = true;
startInfo .RedirectStandardInput = true;
startInfo .UseShellExecute = false;
startInfo .CreateNoWindow = true;
startInfo .Arguments = args;
pros.StartInfo = startInfo ;
pros.ErrorDataReceived += pros_ErrorDataReceived;
pros.OutputDataReceived += pros_OutputDataReceived;
pros.Exited += (object sender, EventArgs e) =>
{
if (ProcessExitedEvent != null)
ProcessExitedEvent(pros.Id);
};
pros.EnableRaisingEvents = true;
pros.Start();
pros.BeginOutputReadLine();
pros.BeginErrorReadLine();
return pros.Id;
}
catch (Exception)
{
return -1;
}
}
private void ProcessExitedEvent(int processId)
{
// Copy file
// Delete file
}
This process create file on disk and after ProcessExitedEvent
event fired i want to copy this file to another location and than delete the old file, but although the event fired after the process exit the my code failed to delete the old file because the file is still in use so i want to ensure that my process die by using using block
so where i need to put this block ?
I very much doubt that a using
statement will help here. Process.Dispose
doesn't kill the other process - it just releases the CLR handle to the other process. (To be honest, I don't remember every seeing anyone use a using
statement for Process
- unlike most disposable resources, I wouldn't worry about this one.)
I'm surprised you can't delete the file, but it may be that the OS is just flushing buffers or something as the process dies. You might want to try waiting a second before deleting the file... although that shouldn't really be necessary. Are you sure it's not something like a virus checker that is picking up the file?
See more on this question at Stackoverflow