【问题标题】:Thread is not sleeping when told to do so线程在被告知这样做时没有休眠
【发布时间】:2011-12-23 12:27:08
【问题描述】:

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

预期结果: 创建一个新线程,调用方法 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();
        }
    }
}

【问题讨论】:

  • 标题+1...搞笑:):):)
  • 但是说真的,CallCheckLogic() 线程会 Sleep()。是什么让你认为它没有?您省略了循环部分。
  • 因为,我可以在我的网络监视器上看到正在使用“ChecksObject.CheckXLogic”方法检查的远程端口正在持续(几乎就像这些方法在循环一样)我已经添加了 ChecksObject。 Check1Logic 方法(它们几乎都一样)
  • 目前还不清楚 LoopPorts() 的作用以及“CallCheckLogic 再次运行”的方式/原因
  • 提供的代码中没有循环。显然,显示的代码之外的东西在这里是相关的。

标签: c# .net windows multithreading


【解决方案1】:

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

尝试在Sleep 之前和之后添加断点,你会知道这行代码是否曾经被执行过。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    相关资源
    最近更新 更多