【发布时间】:2013-02-21 10:31:11
【问题描述】:
也许这是一个重复((但我没有找到任何在 Parallel.Foreach 中使用 binnaryformatter 的示例代码。有人可以提供它的示例吗? 代码示例
Parralel.ForEach(files, fileCurr=>
{
using(lib.Accesser("fileType", fileNameSpec))
{
LoadFileData(fileNameSpec,fileCurr,cancellationToken, progressCallback);
}
});
and
LoadFileData(fileNameSpec,fileCurr,cancellationToken, progressCallback)
{
using(lib2.load(fileCurr.name))
{
foreach(var v in fileCurr.include)
{
var objectForSerialization = loadObj(v);
//my code goes below
System.IO.Stream stream = System.IO.File.Open("J:\\volume", FileMode.OpenOrCreate);
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bformatter = new BinaryFormatter();
bformatter.Serialize(stream, objectForSerialization);
stream.Close();
//but this fil will be locked by multyply writes
}
}
}
【问题讨论】:
-
您的序列化和/或反序列化在并行处理中会有问题吗?例如,它是否取决于同一处理队列中的其他对象的属性?你试过什么?我们可以看看一些示例代码吗?
-
问题出在哪里?有什么例外吗?只需将序列化代码放入类似 serialize(xx) 的 Methode 中,然后从并行搜索中调用 thius。只要确保他们不共享任何资源
-
multiply writes in file((我需要一个简单的示例如何sincronize读取和写入