mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-05-21 06:45:39 -06:00
.reference
git-svn-id: svn://ultimatepp.org/upp/trunk@10280 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
parent
2bb38b8f97
commit
473925253a
6 changed files with 46 additions and 60 deletions
|
|
@ -9,6 +9,7 @@ struct Foo {
|
|||
void ActionWithParam(int y) { Cout() << "ActionWithParam: " << x + y << '\n'; }
|
||||
|
||||
Event<> WhenDo;
|
||||
|
||||
void Do() { WhenDo(); }
|
||||
|
||||
Foo(int x = 0) : x(x) {}
|
||||
|
|
@ -22,11 +23,9 @@ void Fn()
|
|||
struct Bar {
|
||||
Foo foo;
|
||||
|
||||
void Action() { Cout() << "foo's Do called\n"; }
|
||||
|
||||
typedef Bar CLASSNAME;
|
||||
|
||||
Bar() { foo.WhenDo = THISBACK(Action); }
|
||||
Bar() {
|
||||
foo.WhenDo = [=] { Cout() << "foo's Do called\n"; };
|
||||
}
|
||||
};
|
||||
|
||||
struct Safe : Pte<Safe> {
|
||||
|
|
@ -61,10 +60,10 @@ CONSOLE_APP_MAIN
|
|||
Cout() << "---------\n";
|
||||
{
|
||||
Safe f;
|
||||
ev4 = pteback(&f, &Safe::Action);
|
||||
Ptr<Safe> p = &f;
|
||||
ev4 = [=] { if(p) p->Action(); };
|
||||
Cout() << "callback valid: " << (bool)ev4 << '\n';
|
||||
ev4();
|
||||
}
|
||||
Cout() << "callback valid: " << (bool)ev4 << '\n';
|
||||
ev4();
|
||||
} // Safe f gets destroyed here, which makes capture 'p' NULL
|
||||
ev4(); // captured 'p' is now NULL, so no Action is called
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,9 +5,6 @@ using namespace Upp;
|
|||
struct App : TopWindow {
|
||||
Thread work;
|
||||
|
||||
void Work();
|
||||
void AskQuit(bool *quit);
|
||||
|
||||
ArrayCtrl list;
|
||||
|
||||
typedef App CLASSNAME;
|
||||
|
|
@ -16,37 +13,33 @@ struct App : TopWindow {
|
|||
~App();
|
||||
};
|
||||
|
||||
void App::AskQuit(bool *quit)
|
||||
{
|
||||
*quit = PromptYesNo("Quit?");
|
||||
}
|
||||
|
||||
void App::Work()
|
||||
{
|
||||
for(;;) {
|
||||
Sleep(1);
|
||||
GuiLock __;
|
||||
if(IsShutdownThreads())
|
||||
break;
|
||||
if(list.GetCount() > 100) {
|
||||
bool quit;
|
||||
Call(PTEBACK1(AskQuit, &quit)); // This is the generic way for any GUI (dlg)
|
||||
// quit = PromptYesNo("Quit?"); // But Prompt has this ability already implemented
|
||||
if(quit) {
|
||||
Break();
|
||||
return;
|
||||
}
|
||||
list.Clear();
|
||||
}
|
||||
list.Add((int64)Random());
|
||||
}
|
||||
}
|
||||
|
||||
App::App()
|
||||
{
|
||||
list.AddColumn("Test");
|
||||
Add(list.SizePos());
|
||||
work.Run(THISBACK(Work));
|
||||
work.Run([=] {
|
||||
for(;;) {
|
||||
Sleep(1);
|
||||
GuiLock __; // After locking GuiLock __, access is allowed to GUI object except opening/closing windows
|
||||
if(IsShutdownThreads())
|
||||
break;
|
||||
if(list.GetCount() > 100) {
|
||||
bool quit;
|
||||
Ptr<Ctrl> p = this;
|
||||
Call([=, &quit] {
|
||||
if(p) // check whether App still exists (e.g. can be closed by close button)
|
||||
quit = PromptYesNo("Quit?");
|
||||
});
|
||||
// quit = PromptYesNo("Quit?"); // (but Prompt has this ability already implemented, so direct call would work too)
|
||||
if(quit) {
|
||||
Break();
|
||||
return;
|
||||
}
|
||||
list.Clear();
|
||||
}
|
||||
list.Add((int64)Random());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
App::~App()
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
description "Using event queue for communication between worker threads and GUI";
|
||||
description "Using event queue for communication between worker threads and GUI\377";
|
||||
|
||||
uses
|
||||
CtrlLib;
|
||||
|
|
|
|||
|
|
@ -5,9 +5,12 @@ using namespace Upp;
|
|||
#define LAYOUTFILE <GuiMT/Divisors.lay>
|
||||
#include <CtrlCore/lay.h>
|
||||
|
||||
struct Divisors : public WithDivisorsLayout<TopWindow> {
|
||||
typedef Divisors CLASSNAME;
|
||||
// This example shows older way how to do multithreded GUI using PostCallback to communicate
|
||||
// between worker thread and main GUI thread.
|
||||
// While PostCallback is still useful in certain circumstances, new applications are
|
||||
// perhaps better off using GuiLock and Call, as demostrated in GuiLock reference example.
|
||||
|
||||
struct Divisors : public WithDivisorsLayout<TopWindow> {
|
||||
volatile Atomic terminated;
|
||||
volatile Atomic threads;
|
||||
|
||||
|
|
@ -42,10 +45,10 @@ void WorkerThread(DivisorsInfo f)
|
|||
r2 = " " + AsString(j) + r2;
|
||||
divisors++;
|
||||
}
|
||||
PostCallback(callback2(f.gui, &Divisors::ShowResult, f.line, "working..." + r1 + r2));
|
||||
PostCallback([=] { f.gui->ShowResult(f.line, "working..." + r1 + r2); });
|
||||
}
|
||||
}
|
||||
PostCallback(callback2(f.gui, &Divisors::ShowResult, f.line, AsString(divisors) + ": " + r1 + r2));
|
||||
PostCallback([=] { f.gui->ShowResult(f.line, AsString(divisors) + ": " + r1 + r2); });
|
||||
AtomicDec(f.gui->threads);
|
||||
}
|
||||
|
||||
|
|
@ -79,7 +82,7 @@ Divisors::Divisors()
|
|||
{
|
||||
CtrlLayout(*this, "Window title");
|
||||
Sizeable().Zoomable();
|
||||
push <<= THISBACK(TestNumber);
|
||||
push << [=] { TestNumber(); };
|
||||
editor.SetFilter(CharFilterDigit);
|
||||
table.AddColumn("Number");
|
||||
table.AddColumn("Divisors");
|
||||
|
|
|
|||
|
|
@ -18,9 +18,7 @@ struct WebCrawler : public WithCrawlerLayout<TopWindow> {
|
|||
|
||||
void ExtractUrls(const String& html, int srci);
|
||||
void ShowPath();
|
||||
void OpenURL(ArrayCtrl *a);
|
||||
|
||||
typedef WebCrawler CLASSNAME;
|
||||
void OpenURL(ArrayCtrl& a);
|
||||
|
||||
public:
|
||||
void Run();
|
||||
|
|
@ -125,9 +123,9 @@ void WebCrawler::ShowPath()
|
|||
path.Add(p[i]);
|
||||
}
|
||||
|
||||
void WebCrawler::OpenURL(ArrayCtrl *a)
|
||||
void WebCrawler::OpenURL(ArrayCtrl& a)
|
||||
{
|
||||
String u = a->GetKey();
|
||||
String u = a.GetKey();
|
||||
WriteClipboardText(u);
|
||||
LaunchWebBrowser(u);
|
||||
}
|
||||
|
|
@ -139,10 +137,10 @@ WebCrawler::WebCrawler()
|
|||
work.AddColumn("Status");
|
||||
finished.AddColumn("Finished");
|
||||
finished.AddColumn("Response");
|
||||
finished.WhenCursor = THISBACK(ShowPath);
|
||||
finished.WhenLeftDouble = THISBACK1(OpenURL, &finished);
|
||||
finished.WhenCursor = [=] { ShowPath(); };
|
||||
finished.WhenLeftDouble = [=] { OpenURL(finished); };
|
||||
path.AddColumn("Path");
|
||||
path.WhenLeftDouble = THISBACK1(OpenURL, &path);
|
||||
path.WhenLeftDouble = [=] { OpenURL(path); };
|
||||
total = 0;
|
||||
Zoomable().Sizeable();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,8 +11,6 @@ using namespace Upp;
|
|||
#include <Draw/iml_source.h>
|
||||
|
||||
struct MyApp : TopWindow {
|
||||
typedef MyApp CLASSNAME;
|
||||
|
||||
DropList method;
|
||||
|
||||
void Paint(Draw& w) {
|
||||
|
|
@ -40,11 +38,6 @@ struct MyApp : TopWindow {
|
|||
w.DrawText(GetSize().cx - 200, 40, String().Cat() << "Elapsed " << tm << "s");
|
||||
}
|
||||
|
||||
void Sync()
|
||||
{
|
||||
Refresh();
|
||||
}
|
||||
|
||||
MyApp() {
|
||||
SetRect(0, 0, 600, 800);
|
||||
Sizeable();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue