Skip to content

Commit b7d01a0

Browse files
committed
Use gio::spawn_blocking instead of thread::spawn
1 parent 01ad648 commit b7d01a0

File tree

4 files changed

+9
-8
lines changed

4 files changed

+9
-8
lines changed

book/listings/main_event_loop/1/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::thread;
12
use std::time::Duration;
23

34
use gtk::prelude::*;
@@ -30,7 +31,7 @@ fn build_ui(app: &Application) {
3031
button.connect_clicked(move |_| {
3132
// GUI is blocked for 5 seconds after the button is pressed
3233
let five_seconds = Duration::from_secs(5);
33-
std::thread::sleep(five_seconds);
34+
thread::sleep(five_seconds);
3435
});
3536

3637
// Create a window

book/listings/main_event_loop/2/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::thread;
22
use std::time::Duration;
33

44
use gtk::prelude::*;
5-
use gtk::{self, glib, Application, ApplicationWindow, Button};
5+
use gtk::{self, gio, glib, Application, ApplicationWindow, Button};
66

77
const APP_ID: &str = "org.gtk_rs.MainEventLoop2";
88

@@ -31,7 +31,7 @@ fn build_ui(app: &Application) {
3131
// Connect to "clicked" signal of `button`
3232
button.connect_clicked(move |_| {
3333
// The long running operation runs now in a separate thread
34-
thread::spawn(move || {
34+
gio::spawn_blocking(move || {
3535
let five_seconds = Duration::from_secs(5);
3636
thread::sleep(five_seconds);
3737
});

book/listings/main_event_loop/3/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::time::Duration;
33

44
use glib::{clone, MainContext, Priority};
55
use gtk::prelude::*;
6-
use gtk::{glib, Application, ApplicationWindow, Button};
6+
use gtk::{gio, glib, Application, ApplicationWindow, Button};
77

88
const APP_ID: &str = "org.gtk_rs.MainEventLoop3";
99

@@ -34,7 +34,7 @@ fn build_ui(app: &Application) {
3434
button.connect_clicked(move |_| {
3535
let sender = sender.clone();
3636
// The long running operation runs now in a separate thread
37-
thread::spawn(move || {
37+
gio::spawn_blocking(move || {
3838
// Deactivate the button until the operation is done
3939
sender.send(false).expect("Could not send through channel");
4040
let ten_seconds = Duration::from_secs(10);

book/src/main_event_loop.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ but it is not unusual wanting to run a slightly longer operation in one go.
2929

3030
## How to Avoid Blocking the Main Loop
3131

32-
In order to avoid blocking the main loop we can spawn a new thread and let the operation run there.
32+
In order to avoid blocking the main loop we can spawn a new thread with [`gio::spawn_blocking`](https://gtk-rs.org/gtk-rs-core/stable/latest/docs/gio/fn.spawn_blocking.html) and let the operation run there.
3333

3434
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>
3535

@@ -100,7 +100,7 @@ But why did we not do the same thing with our multi-threaded example?
100100
# use std::{thread, time::Duration};
101101
#
102102
# use glib::{clone, MainContext, PRIORITY_DEFAULT};
103-
# use gtk::glib;
103+
# use gtk::{glib, gio};
104104
# use gtk::prelude::*;
105105
# use gtk::{Application, ApplicationWindow, Button};
106106
#
@@ -142,7 +142,7 @@ But why did we not do the same thing with our multi-threaded example?
142142
button.connect_clicked(move |button| {
143143
button.clone();
144144
// The long running operation runs now in a separate thread
145-
thread::spawn(move || {
145+
gio::spawn_blocking(move || {
146146
// Deactivate the button until the operation is done
147147
button.set_sensitive(false);
148148
let five_seconds = Duration::from_secs(5);

0 commit comments

Comments
 (0)