Created
April 25, 2012 09:31
-
-
Save Immerseit/2488503 to your computer and use it in GitHub Desktop.
Sample of BlockingCollection in .NET 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// | |
/// Function in a method that put items to queue | |
/// | |
while (!queue.QueueManager.TryAdd(ResultOfValidation, new Random().Next(20))) | |
Console.WriteLine("Some text output logging because of error."); | |
/// | |
/// Queue Class | |
/// | |
public class Queue | |
{ | |
public BlockingCollection<List<FaultTypes.ValuePair>> QueueManager { get; set; } | |
public Queue() | |
{ | |
QueueHandler(); | |
} | |
public BlockingCollection<List<FaultTypes.ValuePair>> QueueHandler() | |
{ | |
this.QueueManager = new BlockingCollection<List<FaultTypes.ValuePair>>(); | |
Thread sender = new Thread(() => | |
{ | |
List<FaultTypes.ValuePair> faultObj; | |
while (true) | |
{ | |
if (this.QueueManager.TryTake(out faultObj, new Random().Next(20))) | |
{ | |
FixErrors actions; | |
foreach (var item in faultObj) | |
{ | |
actions = new FixErrors(faultObj); | |
Console.WriteLine("Object received. " | |
+ item.ReasonType + " " | |
+ item.Date); | |
} | |
} | |
else | |
{ | |
Thread.Sleep(new Random().Next(20)); | |
} | |
faultObj = null; | |
} | |
}); | |
sender.IsBackground = true; | |
sender.Start(); | |
return QueueManager; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment