C# 如何在邮件批量大小不大于240的情况下创建具有重命名邮件的最后一个文件
在下面的应用程序中C# 如何在邮件批量大小不大于240的情况下创建具有重命名邮件的最后一个文件,c#,C#,在下面的应用程序中 Producer方法将消息添加到阻止的集合中 在消费者方法中,我使用阻塞集合,并将消息添加到列表中,当大小>=240时,将该列表写入json文件 在某种程度上,我在blocking集合中没有任何新消息,但在Consumer中,我有一个大小不>=240的消息列表,在这种情况下,应用程序无法写入新的JSON文件(其余数据) 我如何才能让消费者知道没有新消息出现,请将剩下的内容写入新文件 这可能吗?假设消费者将等待1分钟,如果没有新消息,则将剩余内容写入新文件? 这是代码(这里我添
Producer
方法将消息添加到阻止的集合中
消费者
方法中,我使用阻塞
集合,并将消息
添加到列表
中,当大小>=240时,将该列表写入json文件blocking
集合中没有任何新消息,但在Consumer
中,我有一个大小不>=240的消息列表,在这种情况下,应用程序无法写入新的JSON文件(其余数据)
我如何才能让消费者
知道没有新消息出现,请将剩下的内容写入新文件
这可能吗?假设消费者
将等待1分钟,如果没有新消息,则将剩余内容写入新文件?
这是代码(这里我添加了11条消息。在9条消息之前,批大小为240,它生成了一个文件,但第10和11条消息无法写入新文件)
更新:
class Program
{
private static readonly List<Batch> BatchList = new List<Batch>();
private static readonly BlockingCollection<Message> Messages = new BlockingCollection<Message>();
private const int Maxbatchsize = 240;
private static int _currentsize;
private static void Producer()
{
int ctr = 1;
while (ctr <= 11)
{
Messages.Add(new Message { Id = ctr, Name = $"Name-{ctr}" });
Thread.Sleep(1000);
ctr++;
}
Messages.CompleteAdding();
}
private static void Consumer()
{
foreach (var message in Messages.GetConsumingEnumerable())
{
if (_currentsize >= Maxbatchsize)
{
var listToWrite = new Batch[BatchList.Count];
BatchList.CopyTo(listToWrite);
BatchList.Clear();
_currentsize = 0;
WriteToFile(listToWrite.ToList());
}
else
{
Thread.Sleep(1000);
if (Messages.IsAddingCompleted)
{
var remainSize = Messages.Select(JsonConvert.SerializeObject).Sum(x => x.Length);
if (remainSize == 0)
{
var lastMsg = JsonConvert.SerializeObject(message);
BatchList.Add(new Batch { Message = message });
_currentsize += lastMsg.Length;
Console.WriteLine(lastMsg);
var additionListToWrite = new Batch[BatchList.Count];
BatchList.CopyTo(additionListToWrite);
BatchList.Clear();
_currentsize = 0;
WriteToFile(additionListToWrite.ToList());
break;
}
}
}
var msg = JsonConvert.SerializeObject(message);
BatchList.Add(new Batch { Message = message });
_currentsize += msg.Length;
Console.WriteLine(msg);
}
}
private static void WriteToFile(List<Batch> listToWrite)
{
using (StreamWriter outFile = System.IO.File.CreateText(Path.Combine(@"C:\TEMP", $"{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.json")))
{
outFile.Write(JsonConvert.SerializeObject(listToWrite));
}
}
static void Main(string[] args)
{
var producer = Task.Factory.StartNew(() => Producer());
var consumer = Task.Factory.StartNew(() => Consumer());
Console.Read();
}
}
类程序
{
私有静态只读列表BatchList=新列表();
私有静态只读BlockingCollection消息=新建BlockingCollection();
private const int Maxbatchsize=240;
私有静态int_currentsize;
私有静态无效生成器()
{
int ctr=1;
while(ctr=Maxbatchsize)
{
var listToWrite=新批处理[BatchList.Count];
BatchList.CopyTo(listToWrite);
BatchList.Clear();
_currentsize=0;
WriteToFile(listToWrite.ToList());
}
其他的
{
睡眠(1000);
如果(Messages.IsAddingCompleted)
{
var remainSize=Messages.Select(JsonConvert.SerializeObject).Sum(x=>x.Length);
如果(remainSize==0)
{
var lastMsg=JsonConvert.SerializeObject(消息);
Add(新批{Message=Message});
_currentsize+=最后消息长度;
Console.WriteLine(lastMsg);
var additionListToWrite=新批次[BatchList.Count];
BatchList.CopyTo(additionListToWrite);
BatchList.Clear();
_currentsize=0;
WriteToFile(additionListToWrite.ToList());
打破
}
}
}
var msg=JsonConvert.SerializeObject(消息);
Add(新批{Message=Message});
_currentsize+=消息长度;
控制台写入线(msg);
}
}
私有静态无效写入文件(列表列表写入)
{
使用(StreamWriter outFile=System.IO.File.CreateText(Path.Combine(@“C:\TEMP”,$”{DateTime.Now.ToString(“yyyymmddhhmmssff”)}.json”))
{
Write(JsonConvert.SerializeObject(listToWrite));
}
}
静态void Main(字符串[]参数)
{
var producer=Task.Factory.StartNew(()=>producer());
var consumer=Task.Factory.StartNew(()=>consumer());
Console.Read();
}
}
谢谢,但我无法编译它batchList.Copy()
&listToWrite.Length
这可能吗?假设我将等待1分钟,如果没有进一步的消息,那么写下,新文件中还有什么?请描述一下逻辑?谢谢@NaDeR Star,但它不起作用。只生成1个文件(直到大小限制)和2条消息(9和10)仍然没有写入新文件谢谢,这是部分工作。如果在为以前的结果写入文件后,阻塞集合中只剩下1条消息,则此消息无法写入新文件,但如果有1条以上的消息,则将运行循环,然后remainSize==0
是否满足此条件?假设消费者将等待1分钟,如果没有新消息,则将剩余的内容写入新文件?
public class Message
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Batch
{
public Message Message { get; set; }
}
class Program
{
private static readonly List<Batch> BatchList = new List<Batch>();
private static readonly BlockingCollection<Message> Messages = new BlockingCollection<Message>();
private const int Maxbatchsize = 240;
private static int _currentsize;
private static void Producer()
{
int ctr = 1;
while (ctr <= 11)
{
Messages.Add(new Message { Id = ctr, Name = $"Name-{ctr}" });
Thread.Sleep(1000);
ctr++;
}
Messages.CompleteAdding();
}
private static void Consumer()
{
foreach (var message in Messages.GetConsumingEnumerable())
{
if (_currentsize >= Maxbatchsize)
{
var listToWrite = new Batch[BatchList.Count];
BatchList.CopyTo(listToWrite);
BatchList.Clear();
_currentsize = 0;
WriteToFile(listToWrite.ToList());
}
else
{
Thread.Sleep(1000);
if (Messages.IsAddingCompleted)
{
var remainSize = Messages.Select(JsonConvert.SerializeObject).Sum(x => x.Length);
if (remainSize == 0)
{
var lastMsg = JsonConvert.SerializeObject(message);
BatchList.Add(new Batch { Message = message });
_currentsize += lastMsg.Length;
Console.WriteLine(lastMsg);
var additionListToWrite = new Batch[BatchList.Count];
BatchList.CopyTo(additionListToWrite);
BatchList.Clear();
_currentsize = 0;
WriteToFile(additionListToWrite.ToList());
break;
}
}
}
var msg = JsonConvert.SerializeObject(message);
BatchList.Add(new Batch { Message = message });
_currentsize += msg.Length;
Console.WriteLine(msg);
}
}
private static void WriteToFile(List<Batch> listToWrite)
{
using (StreamWriter outFile = System.IO.File.CreateText(Path.Combine(@"C:\TEMP", $"{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.json")))
{
outFile.Write(JsonConvert.SerializeObject(listToWrite));
}
}
static void Main(string[] args)
{
var producer = Task.Factory.StartNew(() => Producer());
var consumer = Task.Factory.StartNew(() => Consumer());
Console.Read();
}
}