我使用'Task.Factory.ContinueWhenAll()'来提高应用程序的性能,但是我遇到了一个问题,那就是内存正在逐渐变高。
然后我使用GC.collect()和GC.GC.WaitForPendingFinalizers(),但情况相同。
int taskCount = Settings.Default.TaskCount;
int monitorInterval = Settings.Default.MonitorInterval;
while (true)
{
using (TaskHandleSvr taskItem = new TaskHandleSvr())
{
List<Task> taskList = new List<Task>();
for (int i = 0; i < taskCount; i++)
taskList.Add(taskItem.RegisterRun());
await Task.Factory.ContinueWhenAll(taskList.ToArray(), wordCountTasks =>
{
int count = taskItem.ProcessMisseHandledRedisMsg();
string sLogTime = $"{DateTime.UtcNow.AddHours(8).ToString("yyyy-MM-dd HH:mm:ss.ffff")}";
string sLogName = "-";
string sLogMessage = "Service End!";
NLogHelper.Info($"{sLogName}*|*{sLogTime}*|*Loop{count}Msg");
NLogHelper.Info($"{sLogName}*|*{sLogTime}*|*{sLogMessage}");
});
taskItem.Dispose();
GC.Collect();
GC.WaitForPendingFinalizers();
}
//Console.ReadKey();
await Task.Delay(TimeSpan.FromSeconds(5));
}
//下面是TaskHandleSvr
答案 0 :(得分:1)
我在这里!最后,我修复了我的错误。哇〜
感谢楼上的评论,衷心感谢U〜
下面是代码
protected override async Task RunAsync(CancellationToken cancellationToken)
{
int taskCount= Settings.Default.TaskCount;
int monitorIntervalSeconds = Settings.Default.MonitorInterval;
List<Task> taskList = new List<Task>();
while (true)
{
for (int i = 0; i < taskCount; i++)
taskList.Add(Task.Factory.StartNew(fn => { TaskHandleSvr.RegisterRun().Wait(); }, null));
await Task.Factory.ContinueWhenAll(taskList.ToArray(), wordCountTasks =>
{
int count = TaskHandleSvr.ProcessMisseHandledRedisMsg().Result;
string sLogTime = $"{DateTime.UtcNow.AddHours(8).ToString("yyyy-MM-dd HH:mm:ss.ffff")}";
string sLogName = "--myTaskGroup--";
string sLogMessage = "Task End";
NLogHelper.Info($"{sLogName}*|*{sLogTime}*|*RollBack {count} Message");
NLogHelper.Info($"{sLogName}*|*{sLogTime}*|*{sLogMessage}");
});
//Release Resource
foreach (var item in taskList)
item.Dispose();
taskList.RemoveRange(0, taskList.Count);
await Task.Delay(TimeSpan.FromSeconds(monitorIntervalSeconds), cancellationToken);
}
}