FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TCSystemCS/Thread/MultipleTasksExecute.cs at develop · tgoessler/TCSystemCS · GitHub
tgoessler
TCSystemCS
Repository navigation
Code
Pull requests
6
(6)
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
TCSystemCS
/
Thread
/
MultipleTasksExecute.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
101 lines (85 loc) · 2.5 KB
Breadcrumbs
TCSystemCS
/
Thread
/
MultipleTasksExecute.cs
Copy path
File metadata and controls
101 lines (85 loc) · 2.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// *******************************************************************************
//
// ******* *** *** *
// * * * *
// * * * *****
// * * *** * * ** * ** ***
// * * * * * * * **** * * *
// * * * * * * * * * * *
// * *** *** * ** ** ** * *
// *
// *******************************************************************************
// see https://github.com/tgoessler/TCSystemCS for details.
// Copyright (C) 2003 - 2026 Thomas Goessler. All Rights Reserved.
// *******************************************************************************
//
// TCSystem is the legal property of its developers.
// Please refer to the COPYRIGHT file distributed with this source distribution.
//
// *******************************************************************************
#region Usings
using
System
;
using
System
.
Threading
;
using
System
.
Threading
.
Tasks
;
#endregion
namespace
TCSystem
.
Thread
;
internal
sealed
class
MultipleTasksExecute
(
int
_maxNumberOfTasks
)
:
IMultipleTasksExecute
{
#region Public
public
void
ExecuteCommand
(
Action
action
)
{
ExecuteCommandInternal
(
action
,
null
)
;
}
public
void
ExecuteCommand
(
Action
action
,
CancellationToken
token
)
{
ExecuteCommandInternal
(
action
,
token
)
;
}
public
void
WaitAllDone
(
)
{
WaitAllDoneInternal
(
null
)
;
}
public
void
WaitAllDone
(
CancellationToken
token
)
{
WaitAllDoneInternal
(
token
)
;
}
#endregion
#region Private
private
void
WaitAllDoneInternal
(
CancellationToken
?
token
)
{
while
(
_semaphore
.
CurrentCount
!=
_maxNumberOfTasks
)
{
if
(
token
.
HasValue
)
{
Task
.
Delay
(
100
)
.
Wait
(
token
.
Value
)
;
}
else
{
Task
.
Delay
(
100
)
.
Wait
(
)
;
}
}
}
private
void
ExecuteCommandInternal
(
Action
action
,
CancellationToken
?
token
)
{
if
(
token
.
HasValue
)
{
_semaphore
.
Wait
(
token
.
Value
)
;
}
else
{
_semaphore
.
Wait
(
)
;
}
Task
.
Run
(
(
)
=>
{
try
{
action
(
)
;
}
finally
{
_semaphore
.
Release
(
)
;
}
}
)
;
}
private
readonly
SemaphoreSlim
_semaphore
=
new
(
_maxNumberOfTasks
,
_maxNumberOfTasks
)
;
#endregion
}
Back
|
FazBrowse Home
|
New Git URL