本文整理汇总了C#中Amib.Threading.SmartThreadPool类的典型用法代码示例。如果您正苦于以下问题:C# SmartThreadPool类的具体用法?C# SmartThreadPool怎么用?C# SmartThreadPool使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SmartThreadPool类属于Amib.Threading命名空间,在下文中一共展示了SmartThreadPool类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WaitForIdleOnSTPThreadForAnotherWorkItemsGroup
public void WaitForIdleOnSTPThreadForAnotherWorkItemsGroup()
{
SmartThreadPool smartThreadPool = new SmartThreadPool(10*1000, 25, 0);
IWorkItemsGroup workItemsGroup1 = smartThreadPool.CreateWorkItemsGroup(int.MaxValue);
IWorkItemsGroup workItemsGroup2 = smartThreadPool.CreateWorkItemsGroup(int.MaxValue);
workItemsGroup1.QueueWorkItem(
new WorkItemCallback(this.DoSomeWork),
1000);
workItemsGroup1.QueueWorkItem(
new WorkItemCallback(this.DoSomeWork),
1000);
IWorkItemResult wir = workItemsGroup2.QueueWorkItem(
new WorkItemCallback(this.DoWaitForIdle),
workItemsGroup1);
Exception e;
wir.GetResult(out e);
smartThreadPool.Shutdown();
Assert.IsNull(e);
}
示例2: WaitAllT
public void WaitAllT()
{
SmartThreadPool smartThreadPool = new SmartThreadPool();
bool success = true;
IWorkItemResult<int>[] wirs = new IWorkItemResult<int>[5];
for (int i = 0; i < wirs.Length; ++i)
{
wirs[i] = smartThreadPool.QueueWorkItem(new Func<int, int, int>(System.Math.Min), i, i + 1);
}
SmartThreadPool.WaitAll(wirs);
for (int i = 0; i < wirs.Length; ++i)
{
if (!wirs[i].IsCompleted)
{
success = false;
break;
}
int result = wirs[i].GetResult();
if (i != result)
{
success = false;
break;
}
}
smartThreadPool.Shutdown();
Assert.IsTrue(success);
}
示例3: CancelCanceledWorkItem
public void CancelCanceledWorkItem()
{
STPStartInfo stpStartInfo = new STPStartInfo();
stpStartInfo.StartSuspended = true;
SmartThreadPool stp = new SmartThreadPool(stpStartInfo);
IWorkItemResult wir = stp.QueueWorkItem(state => null);
int counter = 0;
wir.Cancel();
try
{
wir.GetResult();
}
catch (WorkItemCancelException ce)
{
ce.GetHashCode();
++counter;
}
Assert.AreEqual(counter, 1);
wir.Cancel();
try
{
wir.GetResult();
}
finally
{
stp.Shutdown();
}
}
示例4: test
public void test()
{
SmartThreadPool smartThreadPool = new SmartThreadPool();
Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL);
GlobalVar.isCrawling = true;
//string conn = string.Format(@"Data Source="+System.Environment.CurrentDirectory+"\\data\\{0}", GlobalVar.filename);
sqlitehelper con = new sqlitehelper(System.Environment.CurrentDirectory + @"\data\" + GlobalVar.filename);
string sql = "select keyword from Content where flag==0";
DataTable dt = new DataTable();
dt = con.GetDataTable(sql);
IWorkItemResult[] wir = new IWorkItemResult[dt.Rows.Count];
MessageBox.Show(dt.Rows.Count.ToString());
for (int i = 0; i < dt.Rows.Count; i++)
{
//MessageBox.Show(dt.Rows[i]["keyword"].ToString());
OneWorker one = new OneWorker(dt.Rows[i]["keyword"].ToString());
ThreadPool.QueueUserWorkItem(one.work, i);
}
bool success = SmartThreadPool.WaitAll(
wir);
if (success)
{
GlobalVar.isCrawling = false;
}
smartThreadPool.Shutdown();
}
示例5: WaitAny
public void WaitAny()
{
SmartThreadPool smartThreadPool = new SmartThreadPool();
IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue);
bool success = false;
IWorkItemResult [] wirs = new IWorkItemResult[5];
for(int i = 0; i < wirs.Length; ++i)
{
wirs[i] =
workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);
}
int index = SmartThreadPool.WaitAny(wirs);
if (wirs[index].IsCompleted)
{
int result = (int)wirs[index].GetResult();
if (1 == result)
{
success = true;
}
}
smartThreadPool.Shutdown();
Assert.IsTrue(success);
}
示例6: TestConcurrencyChange
public void TestConcurrencyChange()
{
SmartThreadPool smartThreadPool = new SmartThreadPool(10 * 1000, 1, 0);
bool success = false;
for (int i = 0; i < 100; ++i)
{
smartThreadPool.QueueWorkItem(
new WorkItemCallback(this.DoSomeWork),
null);
}
smartThreadPool.Concurrency = 1;
success = WaitForMaxThreadsValue(smartThreadPool, 1, 1 * 1000);
Assert.IsTrue(success);
smartThreadPool.Concurrency = 5;
success = WaitForMaxThreadsValue(smartThreadPool, 5, 2 * 1000);
Assert.IsTrue(success);
smartThreadPool.Concurrency = 25;
success = WaitForMaxThreadsValue(smartThreadPool, 25, 4 * 1000);
Assert.IsTrue(success);
smartThreadPool.Concurrency = 10;
success = WaitForMaxThreadsValue(smartThreadPool, 10, 10 * 1000);
Assert.IsTrue(success);
smartThreadPool.Shutdown();
}
示例7: WorkItemWaitCanceling
public void WorkItemWaitCanceling()
{
Assert.Throws<WorkItemTimeoutException>(() =>
{
SmartThreadPool smartThreadPool = new SmartThreadPool();
ManualResetEvent cancelWaitHandle = new ManualResetEvent(false);
// Queue a work item that will occupy the thread in the pool
IWorkItemResult wir1 =
smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);
// Queue another work item that will wait for the first to complete
IWorkItemResult wir2 =
smartThreadPool.QueueWorkItem(new WorkItemCallback(this.SignalCancel), cancelWaitHandle);
try
{
wir1.GetResult(System.Threading.Timeout.Infinite, true, cancelWaitHandle);
}
finally
{
smartThreadPool.Shutdown();
}
});
}
示例8: getImagesAsync
public static void getImagesAsync(List<FilmData> tempFilmList)
{
if (isNetworkAvailable())
{
SmartThreadPool smartThreadPool = new SmartThreadPool(
15 * 1000, // Idle timeout in milliseconds
25, // Threads upper limit
1); // Threads lower limit
var bw = new System.ComponentModel.BackgroundWorker();
bw.DoWork += (s, args) =>
{
foreach (FilmData item in tempFilmList)
{
if (IsolatedStorageHelper.isFileAlreadySaved(item.imageUrl))
ImageHelper.addDownloadedItemsToFilmCollection(item.imageUrl);
else
smartThreadPool.QueueWorkItem(() => getImageFromUrl(item.imageUrl));
}
smartThreadPool.WaitForIdle(); // Wait for the completion of all work items
};
bw.RunWorkerAsync();
}
}
示例9: STPAndWIGStartSuspended
public void STPAndWIGStartSuspended()
{
STPStartInfo stpStartInfo = new STPStartInfo();
stpStartInfo.StartSuspended = true;
SmartThreadPool stp = new SmartThreadPool(stpStartInfo);
WIGStartInfo wigStartInfo = new WIGStartInfo();
wigStartInfo.StartSuspended = true;
IWorkItemsGroup wig = stp.CreateWorkItemsGroup(10, wigStartInfo);
wig.QueueWorkItem(new WorkItemCallback(this.DoWork));
Assert.IsFalse(wig.WaitForIdle(200));
wig.Start();
Assert.IsFalse(wig.WaitForIdle(200));
stp.Start();
Assert.IsTrue(wig.WaitForIdle(5000), "WIG is not idle");
Assert.IsTrue(stp.WaitForIdle(5000), "STP is not idle");
}
示例10: WorkItemWaitCanceling
public void WorkItemWaitCanceling()
{
SmartThreadPool smartThreadPool = new SmartThreadPool();
IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue);
ManualResetEvent cancelWaitHandle = new ManualResetEvent(false);
bool success = false;
// Queue a work item that will occupy the thread in the pool
IWorkItemResult wir1 =
workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null);
// Queue another work item that will wait for the first to complete
IWorkItemResult wir2 =
workItemsGroup.QueueWorkItem(new WorkItemCallback(this.SignalCancel), cancelWaitHandle);
try
{
wir1.GetResult(System.Threading.Timeout.Infinite, true, cancelWaitHandle);
}
catch (WorkItemTimeoutException)
{
success = true;
}
smartThreadPool.Shutdown();
Assert.IsTrue(success);
}
示例11: btnStart_Click
private void btnStart_Click(object sender, RoutedEventArgs e)
{
if (_running)
{
_wakeupEvent.Set();
_stp.Cancel();
_stp.Shutdown(false, 5000);
_stp = null;
btnStart.Content = "Start";
//txtIdleTimeout.Enabled = true;
_running = false;
}
else
{
usageHistoryControl1.Reset();
usageControl1.Value1 = 0;
usageControl1.Value2 = 0;
_wakeupEvent.Set();
_running = true;
btnStart.Content = "Stop";
//txtIdleTimeout.Enabled = false;
STPStartInfo stpStartInfo = new STPStartInfo()
{
MinWorkerThreads = Convert.ToInt32(txtMinThreads.Text),
MaxWorkerThreads = Convert.ToInt32(txtMaxThreads.Text),
IdleTimeout = Convert.ToInt32(txtIdleTimeout.Text) * 1000,
EnableLocalPerformanceCounters = true,
};
_stp = new SmartThreadPool(stpStartInfo);
}
}
示例12: TwoWIGsStartSuspended
public void TwoWIGsStartSuspended()
{
SmartThreadPool stp = new SmartThreadPool();
WIGStartInfo wigStartInfo = new WIGStartInfo();
wigStartInfo.StartSuspended = true;
IWorkItemsGroup wig1 = stp.CreateWorkItemsGroup(10, wigStartInfo);
IWorkItemsGroup wig2 = stp.CreateWorkItemsGroup(10, wigStartInfo);
wig1.QueueWorkItem(new WorkItemCallback(this.DoWork));
wig2.QueueWorkItem(new WorkItemCallback(this.DoWork));
Assert.IsFalse(wig1.WaitForIdle(200));
Assert.IsFalse(wig2.WaitForIdle(200));
wig1.Start();
Assert.IsTrue(wig1.WaitForIdle(200));
Assert.IsFalse(wig2.WaitForIdle(200));
wig2.Start();
Assert.IsTrue(wig1.WaitForIdle(0));
Assert.IsTrue(wig2.WaitForIdle(200));
}
示例13: calculateLabel
private static int calculateLabel(Individual[] weights)
{
Individual[] reversedWeights = { weights[1], weights[0] };
weights[1].ResetScores();
weights[0].ResetScores();
SmartThreadPool smtp = new SmartThreadPool();
for (int index = 0; index < numberOfGames; index++)
{
if (index % 2 == 1)
{
smtp.QueueWorkItem(new WorkItemCallback(threadProc), weights);
}
else
{
smtp.QueueWorkItem(new WorkItemCallback(threadProc), reversedWeights);
}
}
smtp.WaitForIdle();
smtp.Shutdown();
//double strengthRatio = ((double)weights[0].Wins + (double)weights[0].Draws / 2) / numberOfGames;
//return strengthRatio > 0.5 ? 1 : -1;
return weights[0].Wins + 1/2 * weights[0].Draws;
}
示例14: TestWIGConcurrencyChange
public void TestWIGConcurrencyChange()
{
SmartThreadPool smartThreadPool = new SmartThreadPool(10 * 1000, 25, 0);
IWorkItemsGroup wig = smartThreadPool.CreateWorkItemsGroup(smartThreadPool.MaxThreads);
bool success = false;
for (int i = 0; i < 100; ++i)
{
wig.QueueWorkItem(new WorkItemCallback(this.DoSomeLongWork), null);
}
wig.Concurrency = 1;
success = WaitForWIGThreadsInUse(wig, 1, 1 * 1000);
Assert.IsTrue(success);
wig.Concurrency = 5;
success = WaitForWIGThreadsInUse(wig, 5, 2 * 1000);
Assert.IsTrue(success);
wig.Concurrency = 25;
success = WaitForWIGThreadsInUse(wig, 25, 4 * 1000);
Assert.IsTrue(success);
wig.Concurrency = 10;
success = WaitForWIGThreadsInUse(wig, 10, 10 * 1000);
Assert.IsTrue(success);
smartThreadPool.Shutdown();
}
示例15: ExceptionThrowing
public void ExceptionThrowing()
{
SmartThreadPool _smartThreadPool = new SmartThreadPool();
DivArgs divArgs = new DivArgs();
divArgs.x = 10;
divArgs.y = 0;
IWorkItemResult wir =
_smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoDiv), divArgs);
try
{
wir.GetResult();
}
catch(WorkItemResultException wire)
{
Assert.IsTrue(wire.InnerException is DivideByZeroException);
return;
}
catch(Exception e)
{
e.GetHashCode();
Assert.Fail();
}
Assert.Fail();
}