2014-05-21 41 views
0

在一個簡單的表單應用程序中,我在應用程序啓動時運行一個常量線程。在第一次迭代時,一切順利,線程方法「Thread_ContinousChecker」按預期工作。運行一次後,lockChecker.returnBlock()== true命中,然後再次運行。也就是說,不會再嘗試。我有一個預感,這是與await lockChecker.checkTime()行有關,但不明白爲什麼,如果它工作一次,爲什麼它會停止?線程停止運行,無法弄清楚爲什麼

注意:如果Thread_ContinousChecker方法中的第一個if語句命中,也就是說,如果lockChecker.returnBlock()方法爲true,它只會停止工作。如果它是假的,它會繼續。

這裏是我的程序類

static class Program 
{ 

    //Instantiate the lockform 
    static LockForm lockForm; 

    public static bool checkLockForm() 
    { 
     Form checker = Application.OpenForms["LockForm"]; 
     return (checker == null); 
    } 
    public static void toggleLockForm(bool theBool) 
    { 

     //If theBool (our condition) is true start the form 
      if (theBool == true) 
      { 
       //Checks if form already eixsts 
       if (checkLockForm() == true) 
       { 
        //Starts the form 
        Application.Run(lockForm = new LockForm());     
       } 
      } 
     //Now if theBool is false - we want to close down any instances of the form that we may have started 
      if (theBool == false) 
      { 
       //This is saying if an instance of a LockForm exists 
       if (checkLockForm() == false) 
       { 
        //Rest of app does not close but that lockform is disabled. 
        //lockForm.Close(); 
        Application.Restart(); 


       } 
      } 
    } 

    /// <summary> 
    /// The main entry point for the application. 
    /// </summary> 
    [STAThread] 
    static void Main() 
    { 
     MyController cont = new MyController(); 

     //Start new thread for our lock checking 
     Thread thread = new Thread(new ThreadStart(cont.Thread_ContinuousChecker)); 
     thread.IsBackground = true; 
     thread.Name = "Data Polling Thread"; 
     thread.Start(); 

     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     Application.Run(new TrayApp()); 
    } 

    public class MyController 
    { 

     public Boolean checkForm() 
     { 
      if (Process.GetProcessesByName("ControlApp.exe").Length > 0) 
      { 
       // Is running 
       return true; 
      } 
      if (Process.GetProcessesByName("ControlApp.exe").Length == 0) 
      { 
       // Is not running - so start it 
       return false; 
      } 
      return false; 
     } 
     public async void Thread_ContinuousChecker() 
     { 
      while (true) 
      { 

       if (checkForm() == false) 
       { 
        LockLogic lockChecker = new LockLogic(); 
        await lockChecker.checkTime(); 

        if (lockChecker.returnBlock() == true) 
        { 
         Program.toggleLockForm(true); 
        } 
        if (lockChecker.returnBlock() == false) 
        { 
         Program.toggleLockForm(false); 
        } 

       } 
       Thread.Sleep(10000); 

      } 
     } 
    } 

下面是我在上面的程序類,我等候着

public async Task checkTime() 
    { 


     // Read values back from Json file 
     var serializedList = await Task.Run(() => File.ReadAllText(_filePathTimes)); 

     // getting a list of LockTime objects 
     var lockTimeList = await Task.Run(() => (List<LockTime>)JsonConvert.DeserializeObject(serializedList, typeof(List<LockTime>), new JsonSerializerSettings { MissingMemberHandling = MissingMemberHandling.Error })); 

     // 
     if (lockTimeList == null) 
     { 
      return; 
     } 
     if(lockTimeList.Count == 0) 
     { 
      return; 
     } 

      _lockTimes = lockTimeList; 

      //Then I do a foreach loop to go through every value in the start list and add the same located value to my listOfTimes (the list of LockTime objects with start and end) 
      for (int x = 0; x < _lockTimes.Count; x++) 
      { 
       TimeSpan start = new TimeSpan(_lockTimes[x].Start.Hour, _lockTimes[x].Start.Minute, _lockTimes[x].Start.Second); 
       TimeSpan end = new TimeSpan(_lockTimes[x].End.Hour, _lockTimes[x].End.Minute, _lockTimes[x].End.Second); 
       TimeSpan now = new TimeSpan(DateTime.Now.TimeOfDay.Hours, DateTime.Now.TimeOfDay.Minutes, DateTime.Now.TimeOfDay.Seconds); 

       if ((now > start) && (now < end)) 
       { 
        _block = true; 
       } 
       else 
       { 
        _block = false; 
       } 
      } 


    } 

的大規模感謝的人誰能夠發現我的LockLogic的.checkTime()方法出了什麼問題。

回答

0

我有一個預感,問題是你的使用Application.Run(lockForm = new LockForm());。根據http://msdn.microsoft.com/en-us/library/ms157902(v=vs.110).aspx,「此方法爲Closed事件的mainForm參數添加事件處理程序,事件處理程序調用ExitThread來清理應用程序。」

因此,它正在做你所說的 - 將應用程序的生命週期綁定到新創建的LockForm的生命週期中。

希望這會有所幫助。

+0

我試圖通過諸如LockForm foo = new LockForm()等方法來啓動LockForm,它只是不會啓動。保持崩潰,這就是爲什麼我必須去使用Application.Run,​​想辦法解決這個問題嗎? – JARRRRG

+0

通過「崩潰」,我假設你的意思是它沒有顯示窗口或拋出異常,因爲你試圖從後臺線程開始表單。我猜你必須找出一種方法來運行從UI線程創建表單的代碼(http://stackoverflow.com/questions/2367718/automating-the-invokerequired-code-pattern)。 –

+0

我確實看過,但我只是沒有看到發生了什麼。也許我應該在一天左右回來。重新開始。更令人討厭的是,一旦手動關閉表單,線程就會繼續。 – JARRRRG

相關問題