這是沒有答案 - 我不能把所有這些代碼在評論...
這對我的作品。告訴我你的代碼從這個的區別:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.IO;
namespace Test
{
class Program
{
const string OutputFile = @"E:\Output.txt";
object _lock = new object();
static void Main(string[] args)
{
Program program = new Program();
Thread thread = new Thread(program.ThreadMethod);
thread.Start(@"E:\Test.txt");
thread = new Thread(program.ThreadMethod);
thread.Start(@"E:\DoesntExist.txt");
Console.ReadKey();
}
void ThreadMethod(object filename)
{
String result = RunNormal(filename as string);
lock (_lock)
{
FileInfo fi = new FileInfo(OutputFile);
if (!fi.Exists)
{
try
{
fi.Create().Close();
}
catch (System.Security.SecurityException secEx)
{
Console.WriteLine("An exception has occured: {0}", secEx.Message);
return;
}
}
StreamWriter sw = fi.AppendText();
sw.WriteLine(result);
sw.Close();
}
}
string RunNormal(string fullfilename)
{
try
{
Process.Start(fullfilename);
return fullfilename + "|Success";
}
catch (Exception e)
{
return fullfilename + "|" + e.ToString();
}
}
}
}
在Output.txt的輸出是:
E:\DoesntExist.txt|System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified
at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start(String fileName)
at Test.Program.RunNormal(String fullfilename) in E:\Projekti\VS2010\Test\Test\Program.cs:line 59
E:\Test.txt|Success
多少不同的是你的代碼?你打電話給其他方法嗎?你如何處理結果?
你可以試試Console.Writeline(「Success」);相反,看看是否有用? – neeKo 2011-12-30 06:18:49
我試過了。同樣的結果!啓動該進程後,該線程中的所有內容都將停止! – 2011-12-30 06:51:55
嗯,我無法重現這一點...我做了一個線程,打開一個進程,並將消息放入控制檯,它的工作原理應該...您還在做什麼?您可以嘗試在process.start之前放置一個斷點,然後逐步檢查它是否真的停止(因爲它不應該) – neeKo 2011-12-30 07:01:50