C#
- 47 ответов
- 0 вопросов
26
Вклад в тег
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebugType>none</DebugType>
<DebugSymbols>false</DebugSymbols>
</PropertyGroup>
var myEntities = new MyEntity[100];
var maxThreads = 15;
System.Threading.Tasks.Parallel.ForEach(
myEntities,
new System.Threading.Tasks.ParallelOptions { MaxDegreeOfParallelism = maxThreads },
entity =>
{
entity.Do()
});
var myEntities = new MyEntity[100];
var maxThreads = 15;
var semaphoreSlim = new SemaphoreSlim(maxThreads);
var tasks = new List<Task>(myEntities.Length);
foreach (var entity in myEntities)
{
tasks.Add(Task.Run(() =>
{
semaphoreSlim.Wait();
try
{
entity.Do();
}
finally
{
semaphoreSlim.Release();
}
}));
}
Task.WaitAll(tasks.ToArray());
var myEntities = new MyEntity[100];
var maxThreads = 3;
var semaphore = new Semaphore(maxThreads, maxThreads);
var threads = new List<Thread>(myEntities.Length);
foreach (var entity in myEntities)
{
var thread = new Thread(() =>
{
semaphore.WaitOne();
try
{
entity.Do();
}
finally
{
semaphore.Release();
}
});
threads.Add(thread);
thread.Start();
}
foreach (var thread in threads)
thread.Join();
...
private Subject<int> myHotkey = new Subject<int>();
public MainForm()
{
...
myHotkey
.AsObservable()
.Throttle(TimeSpan.FromSeconds(1))
.Subscribe(hotkeyid => MessageBox.Show("Нажата горячая клавиша с ID: " + hotkeyid));
}
...
protected override void WndProc(ref Message keyPressed)
{
...
switch (keyPressed.WParam.ToInt32())
{
case 1:
{
SetPrtSc();
myHotkey.OnNext(keyPressed.WParam.ToInt32());
}
break;
...