C# - 具有不同 Max/Min 线程的 Linq 多线程?
C# - Linq MultiThreading with varying Max/Min Threads?
ThreadPool.SetMinThreads(50, 50);
ServicePointManager.MaxServicePointIdleTime = 8000;
ServicePointManager.DefaultConnectionLimit = 50;
List<Match> Combos = new Regex("^(.{5,}):(.{6,})$", RegexOptions.Multiline).Matches(File.ReadAllText(ofd.FileNames[0])).OfType<Match>().ToList();
var query = Combos.ToObservable().SelectMany(s => Observable.Start(() => new
{
grab = checkall(s.Groups[1].Value.Replace("\n", "").Replace("\r", ""), s.Groups[2].Value.Replace("\n", "").Replace("\r", ""))
})).ObserveOn(this).Do(x =>
{
try
{
TotalChecked.Text = "Tested: " + (int.Parse(TotalChecked.Text.Substring(7)) + 1).ToString();
progressBar2.Value = (int)Math.Round((double)(100 * int.Parse(TotalChecked.Text.Substring(7))) / Combos.Count);
}
catch (Exception)
{
TotalChecked.Text = "Tested: " + (int.Parse(TotalChecked.Text.Substring(7)) + 1).ToString();
progressBar2.Value = (int)Math.Round((double)(100 * int.Parse(TotalChecked.Text.Substring(7))) / Combos.Count);
}
});
query.ToArray().ObserveOn(this).Subscribe(x =>
{
CheckButton.Location = new Point(CheckButton.Location.X + 31, 9);
CheckButton.Width -= 31;
});
上面的代码在文本文件上使用了正则表达式,然后执行了一个函数。
它工作得很好,但现在我希望它执行多个不同的函数,并且只允许在其中一些函数上使用一定数量的线程。
例如:
var query = Combos.ToObservable().SelectMany(s => Observable.Start(() => new
{
grab = checkall(s.Groups[1].Value.Replace("\n", "").Replace("\r", ""), s.Groups[2].Value.Replace("\n", "").Replace("\r", "")),
//This one should be LIMITED to only 5 threads MAX!
grab2 = checktwo(Hi, Hello)
}))
在上面的代码中,我添加了 grab2
,它应该最多分配 5 个线程。
老实说,我不知道如何执行这样的方法。
任何帮助将不胜感激。
如果您需要某些代码仅由 n
个线程执行,您需要一个 semaphore, in this case SemaphoreSlim
就足够了
// 5 threads to run in parallel, all 5 can start immidiately
var semaphore = new SemaphoreSlim(5, 5);
// ...
try
{
semaphore.Wait();
//This one should be LIMITED to only 5 threads MAX!
grab2 = checktwo(Hi, Hello);
}
finally
{
// ensure the release of semaphore
semaphore.Release();
}
你甚至可以在async
methods等待它:
await semaphore.WaitAsync();
ThreadPool.SetMinThreads(50, 50);
ServicePointManager.MaxServicePointIdleTime = 8000;
ServicePointManager.DefaultConnectionLimit = 50;
List<Match> Combos = new Regex("^(.{5,}):(.{6,})$", RegexOptions.Multiline).Matches(File.ReadAllText(ofd.FileNames[0])).OfType<Match>().ToList();
var query = Combos.ToObservable().SelectMany(s => Observable.Start(() => new
{
grab = checkall(s.Groups[1].Value.Replace("\n", "").Replace("\r", ""), s.Groups[2].Value.Replace("\n", "").Replace("\r", ""))
})).ObserveOn(this).Do(x =>
{
try
{
TotalChecked.Text = "Tested: " + (int.Parse(TotalChecked.Text.Substring(7)) + 1).ToString();
progressBar2.Value = (int)Math.Round((double)(100 * int.Parse(TotalChecked.Text.Substring(7))) / Combos.Count);
}
catch (Exception)
{
TotalChecked.Text = "Tested: " + (int.Parse(TotalChecked.Text.Substring(7)) + 1).ToString();
progressBar2.Value = (int)Math.Round((double)(100 * int.Parse(TotalChecked.Text.Substring(7))) / Combos.Count);
}
});
query.ToArray().ObserveOn(this).Subscribe(x =>
{
CheckButton.Location = new Point(CheckButton.Location.X + 31, 9);
CheckButton.Width -= 31;
});
上面的代码在文本文件上使用了正则表达式,然后执行了一个函数。 它工作得很好,但现在我希望它执行多个不同的函数,并且只允许在其中一些函数上使用一定数量的线程。
例如:
var query = Combos.ToObservable().SelectMany(s => Observable.Start(() => new
{
grab = checkall(s.Groups[1].Value.Replace("\n", "").Replace("\r", ""), s.Groups[2].Value.Replace("\n", "").Replace("\r", "")),
//This one should be LIMITED to only 5 threads MAX!
grab2 = checktwo(Hi, Hello)
}))
在上面的代码中,我添加了 grab2
,它应该最多分配 5 个线程。
老实说,我不知道如何执行这样的方法。 任何帮助将不胜感激。
如果您需要某些代码仅由 n
个线程执行,您需要一个 semaphore, in this case SemaphoreSlim
就足够了
// 5 threads to run in parallel, all 5 can start immidiately
var semaphore = new SemaphoreSlim(5, 5);
// ...
try
{
semaphore.Wait();
//This one should be LIMITED to only 5 threads MAX!
grab2 = checktwo(Hi, Hello);
}
finally
{
// ensure the release of semaphore
semaphore.Release();
}
你甚至可以在async
methods等待它:
await semaphore.WaitAsync();