0

I need to run a process multiple times. Each time I call an static method that implement an objects from Process and ProcessStartInfo. The ProcessStartInfo properties has modified to return errors or outputs. Is it possible to call the static method inside Parallel.For loop? I couldn't find any document about thread safety related to this.

public static void Run(string item1, string item2, string item3, string item4) { var ProcInfo = new ProcessStartInfo(Program.exe,(item1+item2+item3+item4)); ProcInfo.CreateNoWindow = true; ProcInfo.UseShellExecute = false; ProcInfo.WorkingDirectory = Environment.CurrentDirectory; ProcInfo.RedirectStandardError = true; var process = Process.Start(ProcInfo); process.WaitForExit(); string error = process.StandardError.ReadToEnd(); int exitCode = process.ExitCode; Console.WriteLine("error>>" + (String.IsNullOrEmpty(error) ? "(none)" : error)); Console.WriteLine("ExitCode: " + exitCode, "ExecuteCommand"); process.Dispose(); } 
2
  • Post your code... it's unlikely that there's anything to stop you calling it (though whether it's sensible or not is another question). But without seeing the code, it's impossible to be sure. Commented May 9, 2013 at 13:12
  • What ProcessInfo class are you talking about? There's no such thing in System.Diagnostics. Look at the documentation for your class. (For example, msdn.microsoft.com/en-us/library/…). At the bottom under "Thread Safety", it will often say "Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe." Commented May 9, 2013 at 13:26

3 Answers 3

2

It all depends on the side effects. Your application it is thread safe since you pass in only string objects and you do not return anything. Because string is immutable and you do no use any static shared member variable this code does run in isolation.

The only issue is that you do write to console. Console.WriteLine is in itself thread safe that only one thread can write at a time to stdout. If you can live that the output of your application does get mixed up then you are already done.

That was the threading part inside your application. But there is now also concurrency happening to the called process. If the started application e.g. does always create a file named xxx.tmp in %TEMP% then you have a race condition which does occur because the called executable can be started concurrently causing file in use errors. When you are sure that the called executable can be safely called concurrently then I would say your code is thread safe and you can call it form as many threads as you like.

Sign up to request clarification or add additional context in comments.

Comments

1

If it's separate processes and not separate threads within the same process, you should look at using a mutex.

3 Comments

I can't see your answer fitting in what the OP asked.
@Marco The question appears to be related to cross-process synchronization. If the OP updates and clarifies exactly what it is they are asking I can refine my answer, however, as it stands my answer is no less relevant than your own.
fair enough, lets see what the OP says
0

Just create a static lock object(or lock the type in case you have one method only) and use the lock() mechanism. Keeping it thread safe depends on your implementation not on the TPL.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.