Skip to content

Instantly share code, notes, and snippets.

@Immerseit
Created April 25, 2012 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Immerseit/2488503 to your computer and use it in GitHub Desktop.
Save Immerseit/2488503 to your computer and use it in GitHub Desktop.
Sample of BlockingCollection in .NET 4
///
/// 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