本文整理汇总了C#中ConcurrentBag.Skip方法的典型用法代码示例。如果您正苦于以下问题:C# ConcurrentBag.Skip方法的具体用法?C# ConcurrentBag.Skip怎么用?C# ConcurrentBag.Skip使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConcurrentBag
的用法示例。
在下文中一共展示了ConcurrentBag.Skip方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main(string[] args)
{
var sw = new Stopwatch();
sw.Start();
var from = DatabaseFactory.CreateMongoDatabase();
var to = DatabaseFactory.CreateMongoDatabase("light-1.com");
if (false) {
var categoryCollection = to.GetCollection<Category>("categories");
categoryCollection.Drop();
categoryCollection.EnsureIndex(IndexKeys.Ascending("Source", "Number"), IndexOptions.SetUnique(true));
var categories = from.GetCollection<Category>("categories").FindAll();
categoryCollection.InsertBatch(categories);
}
var productCollection = to.GetCollection<Product>("products");
productCollection.Drop();
var products = from.GetCollection<Product>("products").FindAll().ToList();
var productsCollection = new List<List<Product>>();
const int BATCH_SIZE = 200;
for (var offset = 0; offset < products.Count; offset += BATCH_SIZE) {
Console.Write(".");
var productsInBatch = products.Skip(offset).Take(BATCH_SIZE).ToList();
productsCollection.Add(productsInBatch);
}
var failedProducts = new ConcurrentBag<Product>();
Parallel.ForEach(productsCollection,
//new ParallelOptions { MaxDegreeOfParallelism = 4 },
productsInBatch => {
try {
Console.Write("+");
productCollection.InsertBatch(productsInBatch);
}
catch {
Console.Write("x");
foreach (var p in productsInBatch)
failedProducts.Add(p);
}
});
for (var offset = 0; offset < failedProducts.Count; offset += BATCH_SIZE) {
Console.Write("=");
var productsInBatch = failedProducts.Skip(offset).Take(BATCH_SIZE).ToList();
productCollection.InsertBatch(productsInBatch);
}
productCollection.EnsureIndex(IndexKeys.Ascending("Source", "Number"), IndexOptions.SetUnique(true));
}
示例2: worker_DoWork
//.........这里部分代码省略.........
pObj.Brand = brand;
pObj.Name = name;
pObj.Price = strPrice;
pObj.OldPrice = strOldPrice;
pObj.Sku = sku;
pObj.Sizes.AddRange(sizeList);
photoStorage.Add(pObj);
Interlocked.Increment(ref totalCount);
}
}
catch (Exception ex)
{
// an error happened
Console.WriteLine(ex.Message);
throw new Exception(ex.Message);
}
}
Interlocked.Increment(ref processedCount);
progressValue = (int)(100.0f / totalCount * processedCount);
self.ReportProgress(progressValue, "Reading products");
});
// in case when folder separation is needed, we create those folders
if (cfg.DirectoryLimit > 0)
{
int numberOfDirectories = (int)Math.Ceiling((double)((float)photoStorage.Count / cfg.DirectoryLimit));
for (int dn = 0; dn < numberOfDirectories; dn++)
{
string subdir = (dn + 1).ToString("D4");
string dirPath = Path.Combine(cfg.Folder, subdir);
if (!Directory.Exists(dirPath)) Directory.CreateDirectory(dirPath);
var part = photoStorage.Skip(cfg.DirectoryLimit * dn).Take(cfg.DirectoryLimit);
foreach (Photo p in part)
p.SubDirectory = subdir;
}
}
// download and update photos
var unprocessedPhotos = photoStorage.Where(p => p.Status == PhotoStatus.New || p.Status == PhotoStatus.Downloaded).ToArray();
while (unprocessedPhotos.Length > 0)
{
Parallel.ForEach(unprocessedPhotos, photo =>
{
// download or convert
string folder = cfg.Folder;
if (!String.IsNullOrEmpty(photo.SubDirectory))
folder = Path.Combine(cfg.Folder, photo.SubDirectory);
string orgname = Path.Combine(folder, "org." + photo.FileName);
string destname = Path.Combine(folder, photo.FileName);
if (photo.Status == PhotoStatus.New)
{
// try download
DownloadResult result = Web.GetFile(photo.Uri, orgname);
switch (result)
{
case DownloadResult.NotFound:
// just skip the picture
photo.Status = PhotoStatus.Rejected;
break;
case DownloadResult.Error:
// try again
self.ReportProgress(progressValue, CONNECTION_ERROR);
Console.WriteLine("Failed {0}", photo.Uri);