【发布时间】:2017-08-19 21:23:07
【问题描述】:
我有这个代码。它给了我一个错误:
目标数组不够长,无法复制 收藏。检查数组索引和长度。
我还以为是用字典的原因,所以切换到ConcurrentDictionary,但是还是出现了错误。
private void SaverCallback()
{
AddThread("Main Thread");
const string path = "milestone";
while (!stop)
{
ConcurrentDictionary<string, object> milestone = new ConcurrentDictionary<string, object>();
milestone.TryAdd("Jobs", JobQueue.Queue.MainQueue);
milestone.TryAdd("Locked Jobs", JobQueue.Queue.LockedQueue);
again: try {
using (FileStream writingStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
{
BinaryFormatter formater = new BinaryFormatter();
formater.Serialize(writingStream, milestone);
writingStream.Flush();
Logger.Debug("Status saved");
}
}
catch(Exception e)
{
Logger.Error($"Milestone exception: {e.Message}");
goto again;
}
this.WaitTime(60000);
}
RemoveThread();
}
UPD:
目标数组不够长,无法复制 收藏。检查数组索引和长度。并且在 System.ThrowHelper.ThrowArgumentException(异常资源资源) 在 System.Collections.Generic.Dictionary
2.CopyTo(KeyValuePair2[] 数组,Int32 索引)在 System.Collections.Generic.Dictionary`2.GetObjectData(SerializationInfo 信息,StreamingContext 上下文)在 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(对象 obj,ISurrogateSelector surrogateSelector,StreamingContext 上下文, SerObjectInfoInit serObjectInfoInit,IFormatterConverter 转换器, ObjectWriter objectWriter, SerializationBinder binder) 在 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo 对象信息,名称信息成员名称信息,名称信息类型名称信息)在 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteArrayMember(WriteObjectInfo objectInfo,NameInfo arrayElemTypeNameInfo,对象数据)在 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteArray(WriteObjectInfo objectInfo、NameInfo memberNameInfo、WriteObjectInfo memberObjectInfo) 在 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo 对象信息,名称信息成员名称信息,名称信息类型名称信息)在 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(对象 图,Header[] inHeaders,__BinaryWriter serWriter,布尔 fCheck)
在 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(流 序列化流、对象图、Header[] 标头、布尔 fCheck)
在 AggregateRunner.Enteties.Saver.SaverCallback()
【问题讨论】:
-
不要使用
goto。它表明一个主要的设计问题。唯一允许跳转的语言是 IL 和 ASM -
JobQueue.Queue.MainQueue和JobQueue.Queue.LockedQueue的类型是什么? -
@MatthewWatson 某种线程池
-
那么,作为
milestone序列化的一部分,另一个线程是否可以将项目添加到作业队列中?如果是这样,那么在这个过程中可能发生了一些不好的事情。 -
所以,如果有异常,记录它并重试,使用 goto,无限。听起来不好吗?
标签: c# .net arrays multithreading dictionary