Description:
--error message
Reading from the stream has failed.
this error because of read wmi is too long.
read OSDetails is very slow,sometime is over 30s .
How to repeat:
--check code
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Stopwatch watch = new Stopwatch();
while (true) { watch.Restart();
var searcher = new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
var collection = searcher.Get();
foreach (var mgtObj in collection) {
string os = mgtObj.GetPropertyValue("Caption").ToString();
} watch.Stop();
Console.WriteLine(watch.ElapsedMilliseconds);
if (watch.ElapsedMilliseconds >= 1000) {
Console.WriteLine("-------------");
File.AppendAllText("abc.txt", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") +","+ watch.ElapsedMilliseconds + "\r\n");
}
}
}
}
}
//repeat code
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
while(true){
try{
MySqlConnection conn = new MySqlConnection("server=server;port=;database=dbtest;uid;");
conn.Open();
MySqlCommand cmd = conn.CreateCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "select @@hostname";
cmd.ExecuteNonQuery();
}
catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); }
}
}
}
}
Suggested fix:
[DisplayName("_os_details")]
public string OSDetails
{
get
{
string os = string.Empty;
try
{
//采用取环境变量中的版本就OK绕过了-jiangxf
os = Environment.OSVersion.VersionString;
//这个在win7以上版本不知道为啥会很慢超过30s导致服务器报错
// Reading from the stream has failed.
// var searcher = new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
// var collection = searcher.Get();
// foreach (var mgtObj in collection)
// {
// os = mgtObj.GetPropertyValue("Caption").ToString();
// break;
// }
}
catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); }
return os;
}
}