c# - Thread safety multiple process -
i need run process multiple times. each time call static method implement objects process
, processstartinfo
. processstartinfo
properties has modified return errors or outputs. possible call static method inside parallel.for
loop? couldn't find document thread safety related 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(); }
it depends on side effects. application thread safe since pass in string objects , not return anything. because string immutable , no use static shared member variable code run in isolation.
the issue write console. console.writeline in thread safe 1 thread can write @ time stdout. if can live output of application mixed done.
that threading part inside application. there concurrency happening called process. if started application e.g. create file named xxx.tmp in %temp% have race condition occur because called executable can started concurrently causing file in use errors. when sure called executable can safely called concurrently code thread safe , can call form many threads like.
Comments
Post a Comment