目前这么些暂时没发现数据丢失问题,需要继续验证。
string fullPath = @"C:\Users\cyan\Desktop\bbb.csv";
FileStream fs = new FileStream(fullPath, System.IO.FileMode.Create, System.IO.FileAccess.Write);
StreamWriter ObjWrites = default(StreamWriter);
ObjWrites = new StreamWriter(fs,Encoding.Default);
//在如下双循环里面写入csv,涉及到多个线程共同写入,所以需要安全同步写入,否则会出现数据遗漏现象。
dynamic swline = StreamWriter.Synchronized(ObjWrites);
Parallel.ForEach(list2,i=>
{
string a1 = i.Name;
double a2 = Convert.ToDouble(i.longa);
double a3 = Convert.ToDouble(i.lata);
foreach (var j in list)
{
string b1 = j.Name;
double b2 = Convert.ToDouble(j.longa);
double b3 = Convert.ToDouble(j.lata);
if (a1 != b1)
{
double c1 = Distance(a2, a3, b2, b3);
if (c1 == 0)
{
string txt = string.Concat("并发: ", ",", a1, ",", b1, ",", a2, ",", a3, ",", b2, ",", b3, ",", c1);
swline.WriteLine(txt);
}
}
}
});