线程在被告知这样做的时候没有睡觉

线程在被告知这样做的时候没有睡觉,第1张

概述线程被告知这样做的时候没有睡觉

我有一个方法在C#中创build一个运行方法的线程(CallCheckLogic – 如下所示)尽pipe我已经指定了睡眠发生(Thread.Sleep(60000);)方法(例如ChecksObject.Check1Logic())Keep循环,像睡眠线程方法被忽略。 这对我来说不合逻辑,有人能够解释为什么这种行为正在发生?

预期结果:创build一个新线程,调用方法CallCheckLogic,它自己调用ChecksObject.Check1Logic >> ChecksObject.Check7Logic,然后Thread.Sleep(60000); 被调用,导致线程hibernate60秒,然后循环运行,再次运行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(); } } }

获取计算机的最后一次唤醒时间

在.Net Cli应用程序中显示完成百分比的最佳方法是什么?

.NET中的窗口句柄可以改变它的值吗?

如何防止用户手动更改文件?

版本控制的生产环境

从远程服务器读取证书哈希

使用BluetoothLEDevice.FromIDAsync连接到蓝牙设备会导致错误

如何以编程方式禁用系统设备?

在linux上编译MonoDevelop 5.3时出错

问题与WPF C#应用程序产生两个主要的windows

我没有看到任何循环。 你的CallCheckLogic方法被调用一次,然后结束,线程停止执行。 也许在你的一个CheckXLogic存在一个无限循环,所以你的线程似乎一直在工作,但我可以说这不可能是睡眠呼叫有问题。

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

总结

以上是内存溢出为你收集整理的线程在被告知这样做的时候没有睡觉全部内容,希望文章能够帮你解决线程在被告知这样做的时候没有睡觉所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1285630.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存