Skip to content

Commit

Permalink
Merge 295a6c3 into ac0ec81
Browse files Browse the repository at this point in the history
  • Loading branch information
LilithHafner committed Aug 3, 2022
2 parents ac0ec81 + 295a6c3 commit 7eec431
Showing 1 changed file with 49 additions and 17 deletions.
66 changes: 49 additions & 17 deletions docs/src/manual/async.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Asynchronous UI

Here is an example of an asynchronous update of the user interface. Since
Julia has currently no possibility of multithreading we use a second process
to offload the work. The example is just a proof of principle.
It is possible to perform background computation without interfering with user interface
responsiveness either using separate processes or using multithreading. Use of a separate
process includes slightly more overhead but is also more robust.

Here is an example using a separate process to offload the work.

```julia
using Gtk
using Gtk, Distributed

btn = GtkButton("Start")
sp = GtkSpinner()
Expand All @@ -19,23 +21,53 @@ grid[1:2,2] = ent
id = addprocs(1)[1]

signal_connect(btn, "clicked") do widget
start(sp)
@Gtk.sigatom begin
@async begin
s = @fetchfrom id begin
sleep(4)
return "I am back"
end
@Gtk.sigatom begin
stop(sp)
set_gtk_property!(ent,:text,s)
start(sp)
@async begin
counter = @fetchfrom id begin
stop_time = time() + 3
counter = 0
while time() < stop_time
counter += 1
end
counter
end
stop(sp)
set_gtk_property!(ent, :text, "I counted to $counter in a separate process!")
end
end
end
end

win = GtkWindow(grid, "Progress Bar", 200, 200)
win = GtkWindow(grid, "Distributed", 200, 200)
showall(win)
```

And here is an example using threads. Notice that this example will freeze the UI during
computation unless Julia is run with two or more threads (`julia -t2` on the command line).

```julia
using Gtk

btn = GtkButton("Start")
sp = GtkSpinner()
ent = GtkEntry()

grid = GtkGrid()
grid[1,1] = btn
grid[2,1] = sp
grid[1:2,2] = ent

signal_connect(btn, "clicked") do widget
start(sp)
Threads.@spawn begin
stop_time = time() + 3
counter = 0
while time() < stop_time
counter += 1
end
stop(sp)
set_gtk_property!(ent, :text, "I counted to $counter in a thread!")
end
end

win = GtkWindow(grid, "Threads", 200, 200)
showall(win)
```

0 comments on commit 7eec431

Please sign in to comment.