Skip to content

Commit

Permalink
deploy: 09167fe
Browse files Browse the repository at this point in the history
  • Loading branch information
Hofer-Julian committed Jul 24, 2023
1 parent 618db83 commit 3895cc1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 36 deletions.
35 changes: 18 additions & 17 deletions stable/latest/book/main_event_loop.html
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ <h1 id="the-main-event-loop"><a class="header" href="#the-main-event-loop">The M
That happens when a single task takes too long.
Let's look at one example.</p>
<p>Filename: <a class=file-link href="https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/main_event_loop/1/main.rs">listings/main_event_loop/1/main.rs</a></p>
<pre><code class="language-rust no_run noplayground">use std::time::Duration;
<pre><code class="language-rust no_run noplayground">use std::thread;
use std::time::Duration;

use gtk::prelude::*;
use gtk::{self, glib, Application, ApplicationWindow, Button};
Expand Down Expand Up @@ -215,7 +216,7 @@ <h1 id="the-main-event-loop"><a class="header" href="#the-main-event-loop">The M
button.connect_clicked(move |_| {
// GUI is blocked for 5 seconds after the button is pressed
let five_seconds = Duration::from_secs(5);
std::thread::sleep(five_seconds);
thread::sleep(five_seconds);
});

// Create a window
Expand All @@ -233,13 +234,13 @@ <h1 id="the-main-event-loop"><a class="header" href="#the-main-event-loop">The M
The <code>sleep</code> call is an artificial example,
but it is not unusual wanting to run a slightly longer operation in one go.</p>
<h2 id="how-to-avoid-blocking-the-main-loop"><a class="header" href="#how-to-avoid-blocking-the-main-loop">How to Avoid Blocking the Main Loop</a></h2>
<p>In order to avoid blocking the main loop we can spawn a new thread and let the operation run there.</p>
<p>In order to avoid blocking the main loop we can spawn a new thread with <a href="https://gtk-rs.org/gtk-rs-core/stable/latest/docs/gio/fn.spawn_blocking.html"><code>gio::spawn_blocking</code></a> and let the operation run there.</p>
<p>Filename: <a class=file-link href="https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/main_event_loop/2/main.rs">listings/main_event_loop/2/main.rs</a></p>
<pre><code class="language-rust no_run noplayground"><span class="boring">use std::thread;
</span><span class="boring">use std::time::Duration;
</span><span class="boring">
</span><span class="boring">use gtk::prelude::*;
</span><span class="boring">use gtk::{self, glib, Application, ApplicationWindow, Button};
</span><span class="boring">use gtk::{self, gio, glib, Application, ApplicationWindow, Button};
</span><span class="boring">
</span><span class="boring">const APP_ID: &amp;str = &quot;org.gtk_rs.MainEventLoop2&quot;;
</span><span class="boring">
Expand Down Expand Up @@ -267,7 +268,7 @@ <h2 id="how-to-avoid-blocking-the-main-loop"><a class="header" href="#how-to-avo
</span> // Connect to &quot;clicked&quot; signal of `button`
button.connect_clicked(move |_| {
// The long running operation runs now in a separate thread
thread::spawn(move || {
gio::spawn_blocking(move || {
let five_seconds = Duration::from_secs(5);
thread::sleep(five_seconds);
});
Expand Down Expand Up @@ -304,7 +305,7 @@ <h2 id="how-to-avoid-blocking-the-main-loop"><a class="header" href="#how-to-avo
</span><span class="boring">
</span><span class="boring">use glib::{clone, MainContext, Priority};
</span><span class="boring">use gtk::prelude::*;
</span><span class="boring">use gtk::{glib, Application, ApplicationWindow, Button};
</span><span class="boring">use gtk::{gio, glib, Application, ApplicationWindow, Button};
</span><span class="boring">
</span><span class="boring">const APP_ID: &amp;str = &quot;org.gtk_rs.MainEventLoop3&quot;;
</span><span class="boring">
Expand Down Expand Up @@ -334,7 +335,7 @@ <h2 id="how-to-avoid-blocking-the-main-loop"><a class="header" href="#how-to-avo
button.connect_clicked(move |_| {
let sender = sender.clone();
// The long running operation runs now in a separate thread
thread::spawn(move || {
gio::spawn_blocking(move || {
// Deactivate the button until the operation is done
sender.send(false).expect(&quot;Could not send through channel&quot;);
let ten_seconds = Duration::from_secs(10);
Expand All @@ -348,10 +349,10 @@ <h2 id="how-to-avoid-blocking-the-main-loop"><a class="header" href="#how-to-avo
receiver.attach(
None,
clone!(@weak button =&gt; @default-return glib::ControlFlow::Break,
move |enable_button| {
button.set_sensitive(enable_button);
glib::ControlFlow::Continue
}
move |enable_button| {
button.set_sensitive(enable_button);
glib::ControlFlow::Continue
}
),
);
<span class="boring">
Expand Down Expand Up @@ -426,10 +427,10 @@ <h2 id="how-to-avoid-blocking-the-main-loop"><a class="header" href="#how-to-avo
receiver.attach(
None,
clone!(@weak button =&gt; @default-return glib::ControlFlow::Break,
move |enable_button| {
button.set_sensitive(enable_button);
glib::ControlFlow::Continue
}
move |enable_button| {
button.set_sensitive(enable_button);
glib::ControlFlow::Continue
}
),
);
<span class="boring">
Expand Down Expand Up @@ -499,7 +500,7 @@ <h2 id="how-to-avoid-blocking-the-main-loop"><a class="header" href="#how-to-avo
<pre><pre class="playground"><code class="language-rust no_run compile_fail"><span class="boring">use std::{thread, time::Duration};
</span><span class="boring">
</span><span class="boring">use glib::{clone, MainContext, PRIORITY_DEFAULT};
</span><span class="boring">use gtk::glib;
</span><span class="boring">use gtk::{glib, gio};
</span><span class="boring">use gtk::prelude::*;
</span><span class="boring">use gtk::{Application, ApplicationWindow, Button};
</span><span class="boring">
Expand Down Expand Up @@ -541,7 +542,7 @@ <h2 id="how-to-avoid-blocking-the-main-loop"><a class="header" href="#how-to-avo
button.connect_clicked(move |button| {
button.clone();
// The long running operation runs now in a separate thread
thread::spawn(move || {
gio::spawn_blocking(move || {
// Deactivate the button until the operation is done
button.set_sensitive(false);
let five_seconds = Duration::from_secs(5);
Expand Down
35 changes: 18 additions & 17 deletions stable/latest/book/print.html
Original file line number Diff line number Diff line change
Expand Up @@ -2499,7 +2499,8 @@ <h2 id="adding-signals-to-custom-gobjects"><a class="header" href="#adding-signa
That happens when a single task takes too long.
Let's look at one example.</p>
<p>Filename: <a class=file-link href="https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/main_event_loop/1/main.rs">listings/main_event_loop/1/main.rs</a></p>
<pre><code class="language-rust no_run noplayground">use std::time::Duration;
<pre><code class="language-rust no_run noplayground">use std::thread;
use std::time::Duration;

use gtk::prelude::*;
use gtk::{self, glib, Application, ApplicationWindow, Button};
Expand Down Expand Up @@ -2531,7 +2532,7 @@ <h2 id="adding-signals-to-custom-gobjects"><a class="header" href="#adding-signa
button.connect_clicked(move |_| {
// GUI is blocked for 5 seconds after the button is pressed
let five_seconds = Duration::from_secs(5);
std::thread::sleep(five_seconds);
thread::sleep(five_seconds);
});

// Create a window
Expand All @@ -2549,13 +2550,13 @@ <h2 id="adding-signals-to-custom-gobjects"><a class="header" href="#adding-signa
The <code>sleep</code> call is an artificial example,
but it is not unusual wanting to run a slightly longer operation in one go.</p>
<h2 id="how-to-avoid-blocking-the-main-loop"><a class="header" href="#how-to-avoid-blocking-the-main-loop">How to Avoid Blocking the Main Loop</a></h2>
<p>In order to avoid blocking the main loop we can spawn a new thread and let the operation run there.</p>
<p>In order to avoid blocking the main loop we can spawn a new thread with <a href="https://gtk-rs.org/gtk-rs-core/stable/latest/docs/gio/fn.spawn_blocking.html"><code>gio::spawn_blocking</code></a> and let the operation run there.</p>
<p>Filename: <a class=file-link href="https://github.com/gtk-rs/gtk4-rs/blob/master/book/listings/main_event_loop/2/main.rs">listings/main_event_loop/2/main.rs</a></p>
<pre><code class="language-rust no_run noplayground"><span class="boring">use std::thread;
</span><span class="boring">use std::time::Duration;
</span><span class="boring">
</span><span class="boring">use gtk::prelude::*;
</span><span class="boring">use gtk::{self, glib, Application, ApplicationWindow, Button};
</span><span class="boring">use gtk::{self, gio, glib, Application, ApplicationWindow, Button};
</span><span class="boring">
</span><span class="boring">const APP_ID: &amp;str = &quot;org.gtk_rs.MainEventLoop2&quot;;
</span><span class="boring">
Expand Down Expand Up @@ -2583,7 +2584,7 @@ <h2 id="how-to-avoid-blocking-the-main-loop"><a class="header" href="#how-to-avo
</span> // Connect to &quot;clicked&quot; signal of `button`
button.connect_clicked(move |_| {
// The long running operation runs now in a separate thread
thread::spawn(move || {
gio::spawn_blocking(move || {
let five_seconds = Duration::from_secs(5);
thread::sleep(five_seconds);
});
Expand Down Expand Up @@ -2620,7 +2621,7 @@ <h2 id="how-to-avoid-blocking-the-main-loop"><a class="header" href="#how-to-avo
</span><span class="boring">
</span><span class="boring">use glib::{clone, MainContext, Priority};
</span><span class="boring">use gtk::prelude::*;
</span><span class="boring">use gtk::{glib, Application, ApplicationWindow, Button};
</span><span class="boring">use gtk::{gio, glib, Application, ApplicationWindow, Button};
</span><span class="boring">
</span><span class="boring">const APP_ID: &amp;str = &quot;org.gtk_rs.MainEventLoop3&quot;;
</span><span class="boring">
Expand Down Expand Up @@ -2650,7 +2651,7 @@ <h2 id="how-to-avoid-blocking-the-main-loop"><a class="header" href="#how-to-avo
button.connect_clicked(move |_| {
let sender = sender.clone();
// The long running operation runs now in a separate thread
thread::spawn(move || {
gio::spawn_blocking(move || {
// Deactivate the button until the operation is done
sender.send(false).expect(&quot;Could not send through channel&quot;);
let ten_seconds = Duration::from_secs(10);
Expand All @@ -2664,10 +2665,10 @@ <h2 id="how-to-avoid-blocking-the-main-loop"><a class="header" href="#how-to-avo
receiver.attach(
None,
clone!(@weak button =&gt; @default-return glib::ControlFlow::Break,
move |enable_button| {
button.set_sensitive(enable_button);
glib::ControlFlow::Continue
}
move |enable_button| {
button.set_sensitive(enable_button);
glib::ControlFlow::Continue
}
),
);
<span class="boring">
Expand Down Expand Up @@ -2742,10 +2743,10 @@ <h2 id="how-to-avoid-blocking-the-main-loop"><a class="header" href="#how-to-avo
receiver.attach(
None,
clone!(@weak button =&gt; @default-return glib::ControlFlow::Break,
move |enable_button| {
button.set_sensitive(enable_button);
glib::ControlFlow::Continue
}
move |enable_button| {
button.set_sensitive(enable_button);
glib::ControlFlow::Continue
}
),
);
<span class="boring">
Expand Down Expand Up @@ -2815,7 +2816,7 @@ <h2 id="how-to-avoid-blocking-the-main-loop"><a class="header" href="#how-to-avo
<pre><pre class="playground"><code class="language-rust no_run compile_fail"><span class="boring">use std::{thread, time::Duration};
</span><span class="boring">
</span><span class="boring">use glib::{clone, MainContext, PRIORITY_DEFAULT};
</span><span class="boring">use gtk::glib;
</span><span class="boring">use gtk::{glib, gio};
</span><span class="boring">use gtk::prelude::*;
</span><span class="boring">use gtk::{Application, ApplicationWindow, Button};
</span><span class="boring">
Expand Down Expand Up @@ -2857,7 +2858,7 @@ <h2 id="how-to-avoid-blocking-the-main-loop"><a class="header" href="#how-to-avo
button.connect_clicked(move |button| {
button.clone();
// The long running operation runs now in a separate thread
thread::spawn(move || {
gio::spawn_blocking(move || {
// Deactivate the button until the operation is done
button.set_sensitive(false);
let five_seconds = Duration::from_secs(5);
Expand Down
2 changes: 1 addition & 1 deletion stable/latest/book/searchindex.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion stable/latest/book/searchindex.json

Large diffs are not rendered by default.

0 comments on commit 3895cc1

Please sign in to comment.