C# 每次迭代后,静态C列表的大小都在不断增长
我目前正在调试一个每5分钟运行一次的程序。我的主要问题是试图找到为什么每次我的程序运行时静态列表的大小都会不断增长。目标是保持此列表的大小相同,但每次执行时都使用不同的值更新其项 下面是每5分钟运行一次的类的代码,它设置了我发送给rabbitmq的json消息的值 在方法PushJsonToBroker中,我发现每次执行后compDataList的大小都在增长C# 每次迭代后,静态C列表的大小都在不断增长,c#,arrays,json,arraylist,static,C#,Arrays,Json,Arraylist,Static,我目前正在调试一个每5分钟运行一次的程序。我的主要问题是试图找到为什么每次我的程序运行时静态列表的大小都会不断增长。目标是保持此列表的大小相同,但每次执行时都使用不同的值更新其项 下面是每5分钟运行一次的类的代码,它设置了我发送给rabbitmq的json消息的值 在方法PushJsonToBroker中,我发现每次执行后compDataList的大小都在增长 class Program { private static List<SimulatedData> com
class Program
{
private static List<SimulatedData> compDataList = new List<SimulatedData>();
public static void Main(String[] args)
{
var autoEvent = new AutoResetEvent(false);
var stateTime = new Timer(SimulateData, autoEvent, 1000, 300000);
Console.ReadKey();
}
public static void SimulateData(object state)
{
Console.WriteLine("\n" + DateTime.Now + "\nSimulating Data...");
List<string> compValueList = new List<string>();
compValueList = ReadCompValueRow();
CompChannelValueMatching(compValueList);
PushJsonToBroker();
}
public static void CompChannelValueMatching(List<string> compValueList)
{
try
{
Console.WriteLine("Matching values with channels for Compressor...");
foreach(var c in compChannelListMongo)
{
string value;
string units;
string simTag;
string name;
var channelIndexMongo = compChannelListMongo.IndexOf(c);
if (c.Equals("C1"))
{
value = compValueList.ElementAt<string>(0);
units = compUnitListMongo.ElementAt<string>(channelIndexMongo);
simTag = compTagListMongo.ElementAt<string>(channelIndexMongo);
name = compNameListMongo.ElementAt<string>(channelIndexMongo);
decimal num, x;
if (decimal.TryParse(value, out x))
{
num = x;
}
else
{
num = 0;
}
SetCompValues(units, simTag, name, num);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
public static void SetCompValues(string units, string simTag, string name, decimal num)
{
compDataList.Add(new SimulatedData { units = units, tag = simTag, name = name, value = num });
}
public static void PushJsonToBroker()
{
List<string> thingList = new List<string>();
List<string> assetTypeList = new List<string>();
Simulator.JsonProps data = new Simulator.JsonProps();
DateTime dateTime = DateTime.Now;
var dateValue = dateTime.ToString("yyyyMMddhhmmss.ffffff");
var demoThings = DBConnect.CosmosClient.GetCollection<BsonDocument>("Things");
foreach (var doc in demoThings.Find(x => x["_id"] != "").ToList())
{
thingList.Add(doc["_id"].ToString());
assetTypeList.Add(doc["AssetType"].ToString());
}
try
{
Console.WriteLine("\nCreating Json...");
foreach (var thingNo in thingList)
{
var thingIndex = thingList.IndexOf(thingNo);
var assetType = assetTypeList.ElementAt<string>(thingIndex);
if (assetType.Equals("HSE"))
{
data = new Simulator.JsonProps
{
machineId = thingNo,
trendValues = hseDataList.ToArray(),
messageId = dateValue + "@" + thingNo,
scheduleDate = DateTime.UtcNow
};
}
if(assetType.Equals("Spotlight_Comp"))
{
data = new Simulator.JsonProps
{
machineId = thingNo,
trendValues = compDataList.ToArray(), //problem is here!, every time the program runs the list grows in size instead of staying the same size each time it executes
messageId = dateValue + "@" + thingNo,
scheduleDate = DateTime.UtcNow
};
}
if(assetType.Equals("CPI"))
{
data = new Simulator.JsonProps
{
machineId = thingNo,
trendValues = cpiDataList.ToArray(),
messageId = dateValue + "@" + thingNo,
scheduleDate = DateTime.UtcNow
};
}
string jsonOutput = JsonConvert.SerializeObject(data, Formatting.Indented);
Console.WriteLine("Publishing JSON to broker for " + assetType);
switch (assetType)
{
case "Spotlight_Comp":
tcompChannel.BasicPublish(exchange: "DataEx",
routingKey: "",
basicProperties: null,
body: Encoding.UTF8.GetBytes(jsonOutput));
break;
case "HSE":
thseChannel.BasicPublish(exchange: "DataEx",
routingKey: "",
basicProperties: null,
body: Encoding.UTF8.GetBytes(jsonOutput));
break;
case "CPI":
tcpiChannel.BasicPublish(exchange: "DataEx",
routingKey: "",
basicProperties: null,
body: Encoding.UTF8.GetBytes(jsonOutput));
break;
}
Console.WriteLine("Done publishing...");
}
Console.WriteLine("Done sending data for all assets..." + "\nWaiting to run again...");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
我不确定这个问题是否与列表是静态的有关。但老实说,这个问题一直困扰着我。如果有人对如何运行这个程序有一些意见或指导,并在每次执行后保持列表大小相同但值不同,这将对我有很大帮助 获取数组后,清除列表
data = new Simulator.JsonProps
{
machineId = thingNo,
trendValues = compDataList.ToArray(), //problem is here!, every time the program runs the list grows in size instead of staying the same size each time it executes
messageId = dateValue + "@" + thingNo,
scheduleDate = DateTime.UtcNow
};
compDataList.Clear(); //add this to resolve your Problem
SetCompValues通过向静态列表添加更多项目。添加,这就是列表不断增长的原因,在调用SetCompValues之前,您从未清除它或将其设置为新列表。我建议使用新列表而不是清除。。当您有其他变量引用它时会导致更多问题。因此,当我调用CompChannelValueMatching时,是否应使用compDataList=new List;我想我喜欢compDataList。清晰的方法。而不是必须宣布一个新的名单;
data = new Simulator.JsonProps
{
machineId = thingNo,
trendValues = compDataList.ToArray(), //problem is here!, every time the program runs the list grows in size instead of staying the same size each time it executes
messageId = dateValue + "@" + thingNo,
scheduleDate = DateTime.UtcNow
};
compDataList.Clear(); //add this to resolve your Problem