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;
});
The code above uses regex on a text file and then executes a function. It works just fine, but now I want it to execute multiple DIFFERENT functions and only allow a set amount of threads on some of them.
For example:
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)
}))
In the code above I added grab2
which should have a maximum of 5 threads allocate to it.
To be honest I have no idea how I could execute a method like this. Any help would be appreciated.
If you need to some code gets executed only by n
threads, you need a semaphore , in this case SemaphoreSlim
is enough
// 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();
}
You can even wait for it in async
methods :
await semaphore.WaitAsync();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.