2 textbox 1 button at verdiğin toplanacak miktarı ver parça satısını ver sana toplasın.
namespace OMR.Crawler.Core.Collections
{
using System;
using System.Collections.Concurrent;
using System.Threading;
/// <summary>
/// This class implements a collection of multi-threaded queue.
/// 2012 - OMR
/// </summary>
/// <typeparam name="T"></typeparam>
public class MultiThreadQueue<T> : ConcurrentQueue<T>
{
private int _maxThreadCount;
private int _runningThreadCount;
/// <summary>
/// ctor
/// </summary>
/// <param name="MaxThreadCount">Maximum count of running thread</param>
public MultiThreadQueue(int MaxThreadCount)
{
_maxThreadCount = MaxThreadCount;
}
/// <summary>
/// Runs multi threaded queue.
/// </summary>
/// <param name="action"></param>
public void RunAll(Action<T> action)
{
while (true)
{
int queueCount = this.Count;
// Calculates number of new threads count
var newThreadCount = (
queueCount > (_maxThreadCount - _runningThreadCount)
? (_maxThreadCount - _runningThreadCount)
: queueCount
);
// Creates new threads
for (int i = 0; i < newThreadCount; i++)
{
T currentWork;
if (!this.TryDequeue(out currentWork))
continue;
if (currentWork == null)
continue;
Interlocked.Increment(ref _runningThreadCount);
var thread = new Thread(
new ParameterizedThreadStart((x) => Run(action, currentWork))
);
thread.Start();
}
if (queueCount == 0 && _runningThreadCount == 0)
break;
}
}
private void Run(Action<T> action, T item)
{
action(item);
Interlocked.Decrement(ref _runningThreadCount);
}
}
} private void button1_Click(object sender, EventArgs e)
{
DateTime baslangic = DateTime.Now;
int toplam = 0;
var myQueue = new MultiThreadQueue<int>(Convert.ToInt32(textBox1.Text));
for (int i = 0; i < Convert.ToInt32(textBox2.Text); i++)
{
myQueue.Enqueue(i);
}
myQueue.RunAll((x) =>
{
toplam = toplam + x;
});
DateTime bitis = DateTime.Now;
TimeSpan fark = bitis - baslangic;
MessageBox.Show(fark.TotalSeconds.ToString());
}