本文整理汇总了C#中ConcurrentStack.ToArray方法的典型用法代码示例。如果您正苦于以下问题:C# ConcurrentStack.ToArray方法的具体用法?C# ConcurrentStack.ToArray怎么用?C# ConcurrentStack.ToArray使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConcurrentStack
的用法示例。
在下文中一共展示了ConcurrentStack.ToArray方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Setup
public void Setup()
{
var pages = new ConcurrentStack<IGooglePageModel>();
Parallel.Invoke(() => pages.Push(new GooglePageModel<InternetExplorerGrid>()),
() => pages.Push(new GooglePageModel<FirefoxGrid>()));
var parallelPage = new ParallelPageModel<IGooglePageModel>(pages.ToArray());
_page = parallelPage.Cast();
_page.Search("SQL For .NET Programmers");
}
示例2: Can_handle_rolling_log
public void Can_handle_rolling_log()
{
var file = Path.Combine(".", "testfile2.xml");
if (File.Exists(file)) { File.Delete(file); }
File.WriteAllText(file, _buffer);
var outofbounds = new ConcurrentStack<OutOfBoundsEvent>();
var exceptionWhileReading = new ConcurrentStack<Exception>();
var files = new ConcurrentQueue<LogEntry>();
using (var watcher = new Watcher<LogEntry>(new FileWithPosition(file), new LogEntryParser()).Tap(w=>
{
w.LogEntry += l => { files.Enqueue(l); };
w.OutOfBounds += () => { outofbounds.Push(new OutOfBoundsEvent()); };
w.ExceptionOccurred += e => { exceptionWhileReading.Push(e); };
}))
{
watcher.Init();
File.WriteAllText(file, "");
Thread.Sleep(100/*750*3*/);
Assert.True(outofbounds.ToArray().Length >= 1, "outofbounds>=1");
}
}
示例3: CreateBlock
public static ActionBlock<StatsdMessage> CreateBlock(ITargetBlock<Bucket> target,
IIntervalService intervalService)
{
var rawLines = new ConcurrentStack<Raw>();
var incoming = new ActionBlock<StatsdMessage>(p =>
{
rawLines.Push(p as Raw);
},
Utility.UnboundedExecution());
intervalService.Elapsed += (sender, e) =>
{
if (rawLines.Count == 0)
{
return;
}
var lines = rawLines.ToArray();
rawLines.Clear();
var bucket = new RawBucket(lines, e.Epoch);
target.Post(bucket);
};
return incoming;
}
示例4: RunConcurrentStackTest5_CtorAndCopyToAndToArray
// Instantiates the stack w/ the enumerator ctor and validates the resulting copyto & toarray.
private static bool RunConcurrentStackTest5_CtorAndCopyToAndToArray(int count)
{
TestHarness.TestLog("* RunConcurrentStackTest5_CtorAndCopyToAndToArray()");
int[] arr = new int[count];
for (int i = 0; i < count; i++) arr[i] = i;
ConcurrentStack<int> s = new ConcurrentStack<int>(arr);
// try toarray.
int[] sa1 = s.ToArray();
if (sa1.Length != arr.Length)
{
TestHarness.TestLog(" > ToArray resulting array is diff length: got {0}, wanted {1}",
sa1.Length, arr.Length);
return false;
}
for (int i = 0; i < sa1.Length; i++)
{
if (sa1[i] != arr[count - i - 1])
{
TestHarness.TestLog(" > ToArray returned an array w/ diff contents: got {0}, wanted {1}",
sa1[i], arr[count - i - 1]);
return false;
}
}
int[] sa2 = new int[count];
s.CopyTo(sa2, 0);
if (sa2.Length != arr.Length)
{
TestHarness.TestLog(" > CopyTo(int[]) resulting array is diff length: got {0}, wanted {1}",
sa2.Length, arr.Length);
return false;
}
for (int i = 0; i < sa2.Length; i++)
{
if (sa2[i] != arr[count - i - 1])
{
TestHarness.TestLog(" > CopyTo(int[]) returned an array w/ diff contents: got {0}, wanted {1}",
sa2[i], arr[count - i - 1]);
return false;
}
}
object[] sa3 = new object[count]; // test array variance.
((System.Collections.ICollection)s).CopyTo(sa3, 0);
if (sa3.Length != arr.Length)
{
TestHarness.TestLog(" > CopyTo(object[]) resulting array is diff length: got {0}, wanted {1}",
sa3.Length, arr.Length);
return false;
}
for (int i = 0; i < sa3.Length; i++)
{
if ((int)sa3[i] != arr[count - i - 1])
{
TestHarness.TestLog(" > CopyTo(object[]) returned an array w/ diff contents: got {0}, wanted {1}",
sa3[i], arr[count - i - 1]);
return false;
}
}
return true;
}
示例5: WriteFloodAndBatchTogether
bool WriteFloodAndBatchTogether(CommandProcessorContext context, int timeOut, int batchSize,
int batchThreadCount, int floodThreadCount)
{
string streamId = Guid.NewGuid().ToString();
const string batchMsg = "BasicVerify-Batch-Test-Message";
const string floodMsg = "BasicVerify-Flood-Test-Message";
int batchCount = 0;
int floodCount = 0;
DateTime dt = DateTime.MaxValue;
var errors = new ConcurrentStack<string>();
var threads = new List<Task>();
for (int t = 0; t < batchThreadCount; t++)
{
var task = Task.Factory.StartNew(() =>
{
while (DateTime.Now < dt)
{
try
{
context.Client.EventStores.WriteEventsInLargeBatch(streamId,
Enumerable.Range(0, batchSize).Select(
x =>
{
var bytes = Encoding.UTF8.GetBytes(batchMsg);
return (bytes);
}));
Interlocked.Add(ref batchCount, 1);
}
catch (Exception ex)
{
errors.Push(ex.Message);
}
}
}, TaskCreationOptions.LongRunning | TaskCreationOptions.PreferFairness);
threads.Add(task);
}
for (int t = 0; t < floodThreadCount; t++)
{
var task = Task.Factory.StartNew(() =>
{
while (DateTime.Now < dt)
{
try
{
context.Client.EventStores.WriteEvent(streamId, Encoding.UTF8.GetBytes(floodMsg));
Interlocked.Add(ref floodCount, 1);
}
catch (Exception ex)
{
errors.Push(ex.Message);
}
}
}, TaskCreationOptions.LongRunning | TaskCreationOptions.PreferFairness);
threads.Add(task);
}
dt = DateTime.Now.AddSeconds(timeOut);
Task.WaitAll(threads.ToArray());
context.Log.Info("Add {0} flood messages", floodCount);
context.Log.Info("Add {0} batch", batchCount);
var readErrors = ReadAddMessages(context, streamId, batchMsg, floodMsg, batchCount * batchSize, floodCount).ToArray();
if (readErrors.Any())
errors.PushRange(readErrors);
foreach (var err in errors.ToArray())
context.Log.Error(err);
return errors.Count == 0;
}
示例6: Test5_CtorAndCopyToAndToArray
// Instantiates the stack w/ the enumerator ctor and validates the resulting copyto & toarray.
private static void Test5_CtorAndCopyToAndToArray(int count)
{
int[] arr = new int[count];
for (int i = 0; i < count; i++) arr[i] = i;
ConcurrentStack<int> s = new ConcurrentStack<int>(arr);
// try toarray.
int[] sa1 = s.ToArray();
Assert.Equal(arr.Length, sa1.Length);
for (int i = 0; i < sa1.Length; i++)
{
Assert.Equal(arr[count - i - 1], sa1[i]);
}
int[] sa2 = new int[count];
s.CopyTo(sa2, 0);
Assert.Equal(arr.Length, sa2.Length);
for (int i = 0; i < sa2.Length; i++)
{
Assert.Equal(arr[count - i - 1], sa2[i]);
}
object[] sa3 = new object[count]; // test array variance.
((System.Collections.ICollection)s).CopyTo(sa3, 0);
Assert.Equal(arr.Length, sa3.Length);
for (int i = 0; i < sa3.Length; i++)
{
Assert.Equal(arr[count - i - 1], (int)sa3[i]);
}
}