[英]Constantly Requesting and Collecting Data for a Certain Period of Time
我需要通过发送命令然后读取结果,在X分钟内连续不断地检索数据。 我不确定以下代码是否有效地工作或出于不断检索的目的:
DateTime utcStartTime = DateTime.UtcNow;
while (DateTime.UtcNow <= (utcStartTime.AddSeconds(recordTime))) //Run the read-write function for approximately the time specified
{
try
{
Write("Write.Request"); //Requests the data
Pmeas = Read(); //Reads the returned string. String = "ReadError" if no result is found.
//Note that the following error checker doesn't effect the speed of data collection at a millisecond level (already checked), and is therefore not the cause of any delay.
if (String.IsNullOrEmpty(Pmeas))
{
Pmeas = "ReadError"; //Necessary due to an apparent overwrite caused by the read function - Tells the later output (outside of while loop) that Pmeas experienced a read error
DateTime utcTime = DateTime.UtcNow; //Get the current time in UTC seconds (may need correcting).
pArray[i] = (Pmeas + "\t" + utcTime.ToString("%d") + Environment.NewLine); //Appends the Pmeas of each instance to a string array with a timestamp
}
else
{
DateTime utcTime = DateTime.UtcNow;
pArray[i] = (Pmeas + "\t" + utcTime.ToString("%d") + Environment.NewLine); //Appends the Pmeas of each instance to a string array with a timestamp
}
Pmeas = "ReadError"; //Reset Pmeas to prove in file that Pmeas experienced a read error
}
catch (Exception f) //Catch an exception if try fails
{
Console.WriteLine("{0} Exception caught.", f);
}
i++; //let i grow so that the array can also grow, plus have a variable already available for being having the string being written into a file (not part of question or code-in-question).
}
请注意,所有变量(例如i,Pmeas和pArray)都在循环之前进行了预定义(以防止错误),并且UTC应该以秒为单位(不确定当前语法;十进制精度)。 就像我说的那样,我正在寻找一种方法,该方法使用上面提供的读写功能不断地从另一个源收集数据,并在设定的时间段内连续地进行此操作。 我的简单问题是, 这是收集数据的正确方法还是有更好和/或更有效的方法?
代码上的所有输入都是欢迎的,即使它不能完全回答问题。
字符串连接可能很慢,请尝试创建一个包含DateTime和一个字符串并由pArray代替的类。 如果时间不很严格,请稍后再转换为字符串。 还可以使用秒表记录持续时间,DateTime的最小分辨率约为15ms。
//Declared elsewhere
public class DataPoint
{
public TimeSpan Time {get; set;}
public String Message {get; set;}
}
List<DataPoint> dataPoints = new List<DataPoint>(pArray.Length); //make the default size the same as pArray so we don't waist time growing the list.
Stopwatch duration = new Stopwatch();
DateTime utcStartTime = DateTime.UtcNow;
duration.Start();
DateTime endTime = utcStartTime.AddSeconds(recordTime); //Move this out of the loop so it only does the calculation once.
while (DateTime.UtcNow <= endTime) //Run the read-write function for approximately the time specified
{
try
{
Write("Write.Request"); //Requests the data
Pmeas = Read(); //Reads the returned string. String = "ReadError" if no result is found.
var dataPoint = new DataPoint
{
Time = duration.Elapsed,
Message = Pmeas
};
dataPoints.Add(dataPoint);
Pmeas = "ReadError"; //Reset Pmeas to prove in file that Pmeas experienced a read error
}
catch (Exception f) //Catch an exception if try fails
{
Console.WriteLine("{0} Exception caught.", f);
}
}
//Now that we are out of the time critical section do the slow work of formatting the data.
foreach(var dataPoint in dataPoints)
{
var message = dataPoint.Message;
if (String.IsNullOrEmpty(message))
{
message = "ReadError";
}
pArray[i] = message + "\t" + (utcStartTime + dataPoint.Time).ToString("%d") + Environment.NewLine; //Appends the Pmeas of each instance to a string array with a timestamp
i++; //let i grow so that the array can also grow, plus have a variable already available for being having the string being written into a file (not part of question or code-in-question).
}
但是,这可能是一个最小的更改,获取一个探查器( Visual Studio附带了一个探查器,但我喜欢使用DotTrace ),看看实际上花费最多的时间并将精力集中在那里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.