线程停止运行,无法找出原因

本文关键字:运行 线程 | 更新日期: 2023-09-27 18:04:15

在一个简单的表单应用程序中,当应用程序启动时,我运行一个恒定的线程。在第一次迭代时,一切都很顺利,线程方法"thread_continuouschecker"按预期工作。在它运行一次并且lockChecker.returnBlock() == true命中之后,它不会再次运行。即,不要再尝试了。我有一种预感,这与等待lockChecker.checkTime()行有关,但不明白为什么,如果它工作一次,为什么会停止?

注意:它只停止工作,如果第一个if语句在thread_continuouschecker方法命中,即如果lockChecker.returnBlock()方法为真。如果为假,则继续。

这是我的程序类

    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);
            }
        }
    }

这是我的LockLogic的。checktime()方法,我正在等待上面的程序类

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;
                }
            }

    }

非常感谢任何能发现问题的人。

线程停止运行,无法找出原因

我有一种预感,问题出在你使用Application.Run(lockForm = new LockForm());。根据http://msdn.microsoft.com/en-us/library/ms157902(v=vs.110).aspx,"这个方法为Closed事件的mainForm参数添加了一个事件处理程序。事件处理程序调用ExitThread来清理应用程序。"

所以,它正在做你告诉它的事情——将应用程序的生命周期绑定到新创建的LockForm的生命周期。