c# - 当被告知线程不休眠时

标签 c# .net windows multithreading

我有一个在 C# 中创建线程的方法,该线程运行一个方法(CallCheckLogic - 如下所示)虽然我已经指定要发生 sleep (Thread.Sleep(60000);)方法(例如 ChecksObject.Check1Logic( )) 继续循环,就像 sleep 线程方法被忽略了一样。这对我来说没有逻辑意义,有人能解释为什么会发生这种行为吗?

预期结果: 创建了一个新线程,调用了 CallCheckLogic 方法,该方法本身调用 ChecksObject.Check1Logic >> ChecksObject.Check7Logic,然后是 Thread.Sleep(60000);被调用,导致线程休眠 60 秒,然后运行循环并再次运行 CallCheckLogic。

private void StartCheckerThread()
{
    Thread t = new Thread(o =>{CallCheckLogic();});t.Start();
    running = true;
}

public void CallCheckLogic()
{
    Checks ChecksObject = new Checks();
    ChecksObject.Check1Logic();
    ChecksObject.Check2Logic();
    ChecksObject.Check3Logic();
    ChecksObject.Check4Logic();
    ChecksObject.Check5Logic();
    ChecksObject.Check6Logic();
    ChecksObject.Check7Logic();

    // This method / delegate parses the outfile of "temp" or rather the results of the tests and turns on / off the appropriate light on the GUI
    LightControlDelegate d1 = new LightControlDelegate(lightControl);
    this.BeginInvoke(d1);
    Thread.Sleep(60000);

    //LoopPorts();
}

ChecksObject.Check1Logic 方法:

public void Check1Logic()
{
    // clean up time! 
    File.Create("temp").Dispose();

    // lets grabs the info from the config!
    var lines = File.ReadAllLines("probe_settings.ini");
    var dictionary = lines.Zip(lines.Skip(1), (a, b) => new { Key = a, Value = b })
                          .Where(l => l.Key.StartsWith("#"))
                          .ToDictionary(l => l.Key, l => l.Value);

    // lets define variables and convert the string in the dictionary to int for the sock.connection method!

    int portno1;
    int.TryParse(dictionary["#PortNo1"], out portno1);

    // Convert hostname to IP, performance issue when using an invalid port on a hostname using the TcpClient class! 
    IPAddress[] addresslist = Dns.GetHostAddresses(hostname2);

    foreach (IPAddress theaddress in addresslist)
    {
        // Attempt to create socket and connect to specified port on host
        TcpClient tcP = new System.Net.Sockets.TcpClient();
        try
        {
            tcP.ReceiveTimeout = 1;
            tcP.SendTimeout = 1;
            tcP.Connect(theaddress, portno1);
            tcP.Close();
            StreamWriter sw = File.AppendText("temp");
            sw.Flush(); 
            sw.WriteLine("Check1=Success");
            sw.Close();
        }
        catch
        {
            StreamWriter sw = File.AppendText("temp");
            sw.WriteLine("Check1=Fail");
            sw.Close();
        }
    }
}

最佳答案

我在这里没有看到任何循环。您的 CallCheckLogic 方法被调用一次,然后它结束并且线程停止执行。 也许在您的 CheckXLogic 之一中存在无限循环,因此您的线程似乎一直在工作,但我可以说 Sleep 调用不太可能存在问题。

尝试在 Sleep 之前和之后添加断点,您将知道这行代码是否执行过。

关于c# - 当被告知线程不休眠时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8616071/

相关文章:

.net - 最安全的软件许可方式

c++ - 如何在我的代码中调用默认 Windows 凭据?

c# - 在 WPF .NET Framework 中使用自定义任务管理器时,如何防止进程重复?

c# - 在代码隐藏中调用 javascript

c# - 从 'empty' 控件中为变量赋值,一定有更好的方法!

c# - 字符串作为变量(点符号而不是括号符号)?

c# - 具有部门技能的员工调度算法C#

c++ - 如何知道所使用的微软 C runtime 的版本?

c# - 文字异常

c# - 对象无法从 DBNull 转换为其他类型