Skip to content

Commit 19f0b68

Browse files
Opt, 添加TaskEntry::WaitTask,避免与Wait类重名
1 parent ff29e10 commit 19f0b68

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
@@ -75,12 +75,22 @@ namespace YY
7575
/// <returns></returns>
7676
void __YYAPI Wakeup(_In_ HRESULT _hrCode);
7777

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

8595
bool __YYAPI IsCanceled() const noexcept
8696
{

src/YY/Base/Threading/TaskRunner.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,23 @@ namespace YY
2323
WakeByAddressAll(&hr);
2424
}
2525

26-
bool __YYAPI TaskEntry::Wait(uint32_t _uMilliseconds)
26+
bool __YYAPI TaskEntry::WaitTask(YY::TimeSpan _oTimeout)
2727
{
28+
DWORD _uMilliseconds;
29+
auto _iTimeoutMilliseconds = _oTimeout.GetTotalMilliseconds();
30+
if (_iTimeoutMilliseconds <= 0)
31+
{
32+
_uMilliseconds = 0;
33+
}
34+
else if (_iTimeoutMilliseconds > UINT32_MAX)
35+
{
36+
_uMilliseconds = UINT32_MAX;
37+
}
38+
else
39+
{
40+
_uMilliseconds = (DWORD)_iTimeoutMilliseconds;
41+
}
42+
2843
HRESULT _hrTarget = E_PENDING;
2944
return WaitOnAddress(&hr, &_hrTarget, sizeof(_hrTarget), _uMilliseconds);
3045
}
@@ -262,7 +277,7 @@ namespace YY
262277
{
263278
return _hr;
264279
}
265-
_oWorkEntry.Wait();
280+
_oWorkEntry.WaitTask();
266281
return _oWorkEntry.hr;
267282
}
268283

0 commit comments

Comments
 (0)