我遇到以下问题(在Xamarin / Android应用中使用C#):
UI线程每秒调用一个等待的DoLongTaskAsync函数并返回结果项。
DoLongTaskAsync函数的持续时间可能不同,有时持续2秒,有时仅为0.5秒。
30秒后,我想把结果项目按照与已完成的呼叫相同的顺序排列。
我无法将变量传递给DoLongTaskAsync。
我不能在等待DoLongTaskAsync之后将它们放入列表中,因为持续时间可能不同,结果2可能会在调用1完成之前返回。
我也无法在等待DoLongTaskAsync之前存储一些时间戳,因为它们将从UI线程的每次调用中被覆盖。
解决这个问题的最佳方法是什么?
答案 0 :(得分:1)
Process
class Process
{
private:
STARTUPINFO startInfo;
PROCESS_INFORMATION processInfo;
CHAR pathToExe[EXTENDED_MAX_PATH];
DWORD returnedValue;
public:
/** I don't know how to pass a LPCSTR parameter to the constructor so i simply use
another function to construct the class */
/** I also don't want a global string and to simply modify it before i declare my object */
bool Create(LPCSTR pathToFile)
{
ZeroMemory(&startInfo,sizeof(startInfo));
ZeroMemory(&processInfo,sizeof(processInfo));
ZeroMemory(&pathToExe,sizeof(pathToExe));
ZeroMemory(&returnedValue,sizeof(returnedValue));
strcpy(pathToExe,pathToFile);
bool hasProcessStarted = CreateProcess(pathToFile,NULL,NULL,NULL,FALSE,0,NULL,NULL,&startInfo,&processInfo);
return hasProcessStarted;
}
bool IsRunning()
{
if (GetExitCodeProcess(processInfo.hProcess,&processInfo.dwProcessId))
if (processInfo.dwProcessId == STILL_ACTIVE) return true;
return false;
}
/** Kind of a destructor. I just made another function because its easier to manage all the data*/
/** Also all the Process Class Objects are dynamically allocated so i just simply delete them after Kill() returns true*/
bool Kill(bool skipIfRunning)
{
if (skipIfRunning == true && IsRunning()) return false;
if (TerminateProcess(processInfo.hProcess,0))
{
ZeroMemory(&startInfo,sizeof(startInfo));
ZeroMemory(&processInfo,sizeof(processInfo));
ZeroMemory(&pathToExe,sizeof(pathToExe));
ZeroMemory(&returnedValue,sizeof(returnedValue));
return true;
}
return false;
}
DWORD GetProcessReturnedValue()
{
return processInfo.dwProcessId;
}
};
。
lock