Reputation: 57
how I can download the file from internet in Inno. but please keep in mind that I want the async download. In otherwords I would like the download starts at the background and UI would be responsive.
As far as I know, it doesn't seems that there is any method to do download async in inno script as it doesn't support theads and timer.
could you help me with this or let me know any other way to achieve this?
Upvotes: 2
Views: 1846
Reputation: 3212
It looks like InnoTools Downloader wasn't updated in a long time and doesn't support UNICODE...
There is another project that looks promissing: https://bitbucket.org/mitrich_k/inno-download-plugin It was updated on Dec2016 and supports ANSI & UNICODE versions of InnoSetup. It has examples and the documentation is OK, it even supports multiple languages, like Spanish, Chinesse...
Upvotes: 1
Reputation: 56717
I'm not sure whether that can be done. You might be able to write an extension DLL with two functions:
One that spawns a new thread for the download (passing the download URL and a temporary file name) and another one that you can call regularly to check whether the download is finished or an error occurred.
You'd then call the first function when your setup starts (make sure to not start the download when uninstalling). Call the second method when you require the downloaded file. At some point you'll need to wait for the download to be finished.
Also, thinking about it, another function to cancel the download thread would be useful so you can react to the setup being cancelled.
See this question to find hints on how to call a function in a DLL from Pascal Script.
EDIT
To make more clear what I'd expect the functions to do, I'm trying to make up some pseudo-code (may look a bit like C# ;-) ):
// This is a global variable for the thread
Thread globalDownloadThread = null;
bool threadFinished = false;
bool threadShouldFinish = false;
void StartDownload(string url, string outputFile)
{
globalDownloadThread = new Thread(DownloadThreadMethod);
globalDownloadThread.Start(url, outputFile);
}
void DownloadThreadMethod(string url, string outputFile)
{
bool downloadComplete = false;
bool downloadErrors = false;
while (!threadShouldFinish && !downloadComplete && !downloadErrors)
{
// Download a bit of the file
// Save to the output file
// Store errors
if (error)
downloadErrors = true;
else if (noMoreBytesToDownload)
// When we're done...
downloadComplete = true;
}
threadFinished = true;
}
void CancelThread()
{
threadShouldFinish = true;
globalDownloadThread.Join(); // Wait for thread to finish!
}
bool IsThreadDone()
{
return threadFinished;
}
In Inno Setup, call the StartDownload
method first. If the setup is cancelled after StartDownload
was called (only then there's a thread!), call CancelThread
.
At the point where you need to make sure that the file was downloaded, repeatedly call IsThreadDone
until it returns true.
Of course, this has to be spiced up with locking mechanisms and there's no real error reporting here, but I hope you get the picture of what I had in mind.
EDIT 2
I'll translate some of my pseudo-code into C++ from what you've posted in the comments.
HANDLE hThread = 0;
DWORD dwThreadId = 0;
DWORD dwThreadResult = 0;
extern "C" bool _declspec(dllexport) downloadfile(TCHAR *url, TCHAR *dest)
{
DOWNLOADPARAM *obj = new DOWNLOADPARAM();
obj->szURL = url;
obj->szFilePath = dest;
if ((hThread = CreateThread(NULL, 0, inetTransfer, (LPVOID)obj, 0,&dwThreadId)) == NULL)
{
return FALSE;
}
return TRUE;
}
extern "C" bool _declspec(dllexport) isThreadDone()
{
DWORD res = WaitForSingleObject(hThread, 10);
if (res == WAIT_OBJECT_0)
return TRUE;
// Set error flag depending on other values of res
dwThreadResult = res;
return FALSE;
}
extern "C" DWORD _declspec(dllexport) isThreadErrored()
{
return dwThreadResult;
}
It does not matter whether there's a while
loop in your inetTransfer
function or not - you will most probably even require a loop of some kind! The downloadfile
function creates the thread and returns. The other functions are used to check whether the thread has successfully ended.
Upvotes: 2
Reputation: 24283
Look at InnoTools Downloader. It was written for exactly this purpose and gives download progress, etc..
Upvotes: 2