Home » Developing U++ » UppHub » Coroutines package for U++
Re: Coroutines package for U++ [message #59151 is a reply to message #59126] |
Wed, 09 November 2022 22:52   |
Oblivion
Messages: 1206 Registered: August 2007
|
Senior Contributor |
|
|
Hello,
As per Lance's request, I am adding some "non-trivial" coroutines examples to the package.
To demonstrate its usage, I have started with adapting the uppsrc/reference/GuiWebDownload example, so that it can be compared:
#include <CtrlLib/CtrlLib.h>
#include <CoRoutines/CoRoutines.h>
// GuiWebDownloader example, adapted to coroutines.
using namespace Upp;
CoRoutine<void> HttpDownload(const String& url)
{
Progress pi;
String path = AppendFileName(Nvl(GetDownloadFolder(), GetHomeDirFile("downloads")), GetFileName(url));
HttpRequest http(url);
http.Timeout(0);
int loaded = 0;
{
FileOut out(path);
http.WhenContent = [&out, &loaded](const void *ptr, int size) { out.Put(ptr, size); loaded += size; };
while(http.Do()) {
if(http.GetContentLength() >= 0) {
pi.SetText("Downloading " + GetFileName(url));
pi.Set((int)loaded, (int)http.GetContentLength());
}
else {
pi.Set(0, 0);
pi.SetText(http.GetPhaseName());
}
if(pi.Canceled())
http.Abort();
else
co_await CoSuspend();
}
}
if(http.IsFailure()) {
DeleteFile(path);
Exclamation("Download has failed.&\1" +
(http.IsError()
? http.GetErrorDesc()
: AsString(http.GetStatusCode()) + ' ' + http.GetReasonPhrase()));
}
}
GUI_APP_MAIN
{
StdLogSetup(LOG_COUT|LOG_FILE);
String url = "http://downloads.sourceforge.net/project/upp/upp/4179/upp-x11-src-4179.tar.gz";
for(;;) {
if(!EditText(url, "Download", "URL"))
return;
auto downloader = HttpDownload(url); // Multiple downloaders can be created/run at once.
try
{
while(downloader.Do())
Sleep(1); // Simulate work.
}
catch(const Exc& e)
{
}
catch(...)
{
}
}
}
Best regards,
Oblivion
Github page: https://github.com/ismail-yilmaz
upp-components: https://github.com/ismail-yilmaz/upp-components
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
[Updated on: Wed, 09 November 2022 22:56] Report message to a moderator
|
|
|
 |
|
Coroutines package for U++
By: Oblivion on Sat, 05 November 2022 12:19
|
 |
|
Re: Coroutines package for U++
By: Oblivion on Sun, 06 November 2022 20:20
|
 |
|
Re: Coroutines package for U++
By: Klugier on Sun, 06 November 2022 20:44
|
 |
|
Re: Coroutines package for U++
By: Oblivion on Sun, 06 November 2022 21:31
|
 |
|
Re: Coroutines package for U++
By: Lance on Mon, 07 November 2022 00:14
|
 |
|
Re: Coroutines package for U++
By: koldo on Mon, 07 November 2022 08:39
|
 |
|
Re: Coroutines package for U++
By: Oblivion on Mon, 07 November 2022 20:07
|
 |
|
Re: Coroutines package for U++
By: peterh on Mon, 07 November 2022 20:30
|
 |
|
Re: Coroutines package for U++
By: Oblivion on Mon, 07 November 2022 20:31
|
 |
|
Re: Coroutines package for U++
By: peterh on Tue, 08 November 2022 09:09
|
 |
|
Re: Coroutines package for U++
By: Oblivion on Wed, 09 November 2022 16:22
|
 |
|
Re: Coroutines package for U++
By: Oblivion on Wed, 09 November 2022 22:52
|
 |
|
Re: Coroutines package for U++
By: Lance on Wed, 09 November 2022 23:26
|
 |
|
Re: Coroutines package for U++
By: Lance on Sat, 12 November 2022 00:33
|
 |
|
Re: Coroutines package for U++
By: Oblivion on Sat, 12 November 2022 07:22
|
 |
|
Re: Coroutines package for U++
By: Lance on Sun, 13 November 2022 05:37
|
 |
|
Re: Coroutines package for U++
By: Lance on Mon, 14 November 2022 02:17
|
 |
|
Re: Coroutines package for U++
By: Oblivion on Mon, 14 November 2022 05:56
|
 |
|
Re: Coroutines package for U++
By: Lance on Mon, 14 November 2022 15:08
|
 |
|
Re: Coroutines package for U++
By: peterh on Mon, 07 November 2022 18:38
|
 |
|
Re: Coroutines package for U++
By: zsolt on Mon, 12 December 2022 13:48
|
 |
|
Re: Coroutines package for U++
By: Oblivion on Mon, 12 December 2022 17:42
|
 |
|
Re: Coroutines package for U++
By: zsolt on Mon, 12 December 2022 21:38
|
Goto Forum:
Current Time: Sat May 10 13:55:03 CEST 2025
Total time taken to generate the page: 0.03268 seconds
|