2014-05-21 42 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

相关问题