스레드와 함께 작업 한 것은 처음이므로 Thread.CurrentThread.Name의 값이 null 인 이유가 무엇인지 혼동합니다. 각각의 특정 스레드가 자체의 타이머를 갖기를 원하고 각 스레드가 실행 중인지 먼저 테스트하고 싶습니다. OnElapsed 메소드. 그러나 Visual Studio를 사용하여 디버깅 할 때 스레드 이름이 null 인 것 같습니다. 어떤 도움을 주시면 감사하겠습니다.왜 스레드 이름이 null입니까?
public class Sample {
static System.Timers.Timer myTimer = new System.Timers.Timer();
static int i = 0;
public static void Main()
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"C:\Users\Documents";
watcher.NotifyFilter = NotifyFilters.LastWrite;
Thread.CurrentThread.Name = "main";
watcher.Filter = "*.txt";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
Console.WriteLine("Press the Enter key to exit the program.");
Console.ReadLine();
}
private static void OnElapsed(object source, ElapsedEventArgs e)
{
Console.WriteLine(Thread.CurrentThread.Name); //<-- why is this null?
myTimer.Enabled = false;
Console.WriteLine(e.SignalTime);
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
FileSystemWatcher t = source as FileSystemWatcher;
t.EnableRaisingEvents = false;
string path = e.FullPath;
Thread t1 = new Thread(() => Print(path));
t1.Name = "Thread " + i;
i++;
t1.IsBackground = true;
t1.Start();
t.EnableRaisingEvents = true;
}
static void Print(string source)
{
string xmlFilePath = "xmlBasic.xml";
string timeout;
string path = Path.GetDirectoryName(source);
List<string> wordList = new List<string>();
XmlDocument doc = new XmlDocument();
doc.Load(xmlFilePath);
XmlNode timeouts;
XmlElement root = doc.DocumentElement;
timeouts = root.SelectSingleNode("descendant::paths[directory='" + path + "']");
timeout = timeouts.LastChild.InnerText;
myTimer.Interval = int.Parse(timeout);
myTimer.Elapsed += new ElapsedEventHandler(OnElapsed);
myTimer.Enabled = true;
}
}
미리 감사드립니다.
내가이 일에 대해 잘 모르는입니다하지만 난 궁금 당신이 기대하는 가치는 무엇입니까? 보시다시피 메인 스레드의 이름을 지정하고 있지만 이벤트 발생에 의해 스핀 업되는 스레드는 아무 것도 명시 적으로 지정되지 않습니다 ... – Chris
System.Timers.Timer는 Elapsed 이벤트를 다른 스레드에서 발생시키기 때문에 (분명히?) 스레드 이름을 설정 한 곳이 아닙니다. –