Guten Tag zusammen,
hat jemand vielleicht eine schönere Variante für konstante Loops in Threads, die bis zum Ende der Laufzeit des Mainthreads laufen?
Mir schwebt da irgendwie while(mainThread.IsAlive) vor.
Danke im voraus!
Mit freundlichen Grüßen,
rAyt
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85:
| namespace FolderWatch_Service { public partial class FolderWatch_Service : ServiceBase { DataTable dt_watch_folders = new DataTable("watch_folders");
public FolderWatch_Service() { InitializeComponent(); }
protected override void OnStart(string[] args) { try { create_datatable(); dt_watch_folders.ReadXml("c:\\folders.xml"); this.doActions(dt_watch_folders); } catch (Exception e) { Console.WriteLine("Error: Cannot start Service Please check WatchFolder XML (First Argument for the Service)" + e.ToString()); } }
private void doActions(DataTable dt_watch_folders) { foreach (DataRow dr in dt_watch_folders.Rows) { MyThreads m = new MyThreads(); m.source_folder = dr["source_folder"].ToString(); m.target_folder = dr["target_folder"].ToString(); m.sleep_time = Convert.ToInt32(dr["recheck_time"].ToString()); Thread t = new Thread(new ThreadStart(m.thread_moving)); t.IsBackground = true; t.Name = "Moving Threads"; t.Start(); } } private void create_datatable() { this.dt_watch_folders.Columns.Add("source_folder"); this.dt_watch_folders.Columns.Add("target_folder"); this.dt_watch_folders.Columns.Add("action"); this.dt_watch_folders.Columns.Add("recheck_time"); }
class MyThreads { public string source_folder; public string target_folder; public int sleep_time;
public void thread_moving() { while (true) { try { DirectoryInfo dir = new DirectoryInfo(source_folder); FileInfo[] files = dir.GetFiles(); foreach (FileInfo aFile in files) { string newtarget = target_folder + "\\" + aFile.Name; aFile.MoveTo(newtarget); Console.WriteLine(newtarget); }
} catch (Exception e) { Console.WriteLine("Error: Could not move data: " + e.ToString()); } Thread.Sleep(sleep_time); } } }
protected override void OnStop() { } } } |