Skip to content

Commit 35907c6

Browse files
Fea, 添加TaskEntry::WaitTask,避免与Wait类重名
1 parent 0740dcd commit 35907c6

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

include/YY/Base/Threading/TaskRunner.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,22 @@ namespace YY
7777
/// <returns></returns>
7878
void __YYAPI Wakeup(_In_ HRESULT _hrCode);
7979

80+
/// <summary>
81+
/// 阻塞等待任务完成或直到达到指定超时时间(默认无限等待)。
82+
/// </summary>
83+
/// <param name="_oTimeout">最大等待时间,类型为 YY::TimeSpan。默认值为 YY::TimeSpan::GetMax(),表示无限期等待。</param>
84+
/// <returns>布尔值:如果在指定超时时间内等待成功(任务完成等)则返回 true;如果超时则返回 false。</returns>
85+
bool __YYAPI WaitTask(YY::TimeSpan _oTimeout = YY::TimeSpan::GetMax());
86+
8087
/// <summary>
8188
/// 等待此任务完成。
8289
/// </summary>
8390
/// <param name="_uMilliseconds">需要等待的毫秒数。</param>
8491
/// <returns></returns>
85-
bool __YYAPI Wait(_In_ uint32_t _uMilliseconds = UINT32_MAX);
92+
bool __YYAPI Wait(_In_ uint32_t _uMilliseconds = UINT32_MAX)
93+
{
94+
return WaitTask(YY::TimeSpan::FromMilliseconds(_uMilliseconds));
95+
}
8696

8797
bool __YYAPI IsCanceled() const noexcept
8898
{

src/YY/Base/Threading/TaskRunner.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,23 @@ namespace YY
3030
}
3131
}
3232

33-
bool __YYAPI TaskEntry::Wait(uint32_t _uMilliseconds)
33+
bool __YYAPI TaskEntry::WaitTask(YY::TimeSpan _oTimeout)
3434
{
35+
DWORD _uMilliseconds;
36+
auto _iTimeoutMilliseconds = _oTimeout.GetTotalMilliseconds();
37+
if (_iTimeoutMilliseconds <= 0)
38+
{
39+
_uMilliseconds = 0;
40+
}
41+
else if (_iTimeoutMilliseconds > UINT32_MAX)
42+
{
43+
_uMilliseconds = UINT32_MAX;
44+
}
45+
else
46+
{
47+
_uMilliseconds = (DWORD)_iTimeoutMilliseconds;
48+
}
49+
3550
HRESULT _hrTarget = E_PENDING;
3651
return WaitOnAddress(&hr, &_hrTarget, sizeof(_hrTarget), _uMilliseconds);
3752
}
@@ -289,7 +304,7 @@ namespace YY
289304
{
290305
return _hr;
291306
}
292-
_oWorkEntry.Wait();
307+
_oWorkEntry.WaitTask();
293308
return _oWorkEntry.hr;
294309
}
295310

0 commit comments

Comments
 (0)