# How to avoid "Julia is not responding" when using a file dialog

**URL:** <https://discourse.julialang.org/t/how-to-avoid-julia-is-not-responding-when-using-a-file-dialog/112075>\
**Category:** Visualization\
**Tags:** question, package, multithreading, glmakie\
**Created:** [March 25, 2024, 10:19am UTC](https://discourse.julialang.org/t/how-to-avoid-julia-is-not-responding-when-using-a-file-dialog/112075 "2024-03-25T10:19:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [March 25, 2024, 10:19am UTC](https://discourse.julialang.org/t/how-to-avoid-julia-is-not-responding-when-using-a-file-dialog/112075/1 "2024-03-25T10:19:13Z")

</div>

I have a GLMakie based GUI app and want to load a file using a native file dialog. This is my code:

```julia
using GLMakie, NativeFileDialog

fig = Figure(size=(200, 200), backgroundcolor=RGBf(0.7, 0.8, 1))
btn_RUN = Button(fig, label = " Open file... ")
on(btn_RUN.clicks) do c;
    println("opening file dialog...")
    filename = pick_file(pwd())
    println(filename)
end
fig

```

While it works, I get the message “Julia is not responding…” if I keep the dialog open for more than 5 or 10 seconds.

How can I avoid that?

My idea would be to run the dialog in a separate thread, but how can that be achieved?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [March 25, 2024, 10:42am UTC](https://discourse.julialang.org/t/how-to-avoid-julia-is-not-responding-when-using-a-file-dialog/112075/2 "2024-03-25T10:42:57Z")

</div>

I am one step further:

```julia
using KiteViewers.GLMakie, NativeFileDialog

fig = Figure(size=(200, 200), backgroundcolor=RGBf(0.7, 0.8, 1))
btn_RUN = Button(fig, label = " Open file... ")
on(btn_RUN.clicks) do c;
    println("opening file dialog...")
    Threads.@spawn begin
        filename = pick_file("")
        println(filename)
    end
end
fig

```

But how can I read the filename in the main thread in a thread-safe manner?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [March 25, 2024, 10:57am UTC](https://discourse.julialang.org/t/how-to-avoid-julia-is-not-responding-when-using-a-file-dialog/112075/3 "2024-03-25T10:57:57Z")

</div>

This seams to work:

```julia
using KiteViewers.GLMakie, NativeFileDialog

fig = Figure(size=(200, 200), backgroundcolor=RGBf(0.7, 0.8, 1))
btn_RUN = Button(fig, label = " Open file... ")
lk = ReentrantLock()
filename = ""
on(btn_RUN.clicks) do c;
    println("opening file dialog...")
    Threads.@spawn begin
        global filename
        lock(lk) do
            filename = pick_file("")
        end
    end
end
@async begin
    while true
        global filename
        lock(lk) do
            if filename != ""
                println(filename)
                filename = ""
            end
        end
        sleep(0.1)
    end
end
fig

```

---

<div class="post-metadata">

**Author:** ![screw\_dog](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/screw_dog/32/48119_2.png) [@screw\_dog](https://discourse.julialang.org/u/screw_dog)\
**Post date:** [March 25, 2024, 11:53am UTC](https://discourse.julialang.org/t/how-to-avoid-julia-is-not-responding-when-using-a-file-dialog/112075/4 "2024-03-25T11:53:44Z")

</div>

Does `fetch`ing the spawned task make Julia unresponsive? If not

```julia
filename = fetch(Threads.@spawn pick_file(""))

```

should accomplish the same.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [March 25, 2024, 12:11pm UTC](https://discourse.julialang.org/t/how-to-avoid-julia-is-not-responding-when-using-a-file-dialog/112075/5 "2024-03-25T12:11:30Z")

</div>

Well, this does NOT work, it still blocks the main thread:

```julia
using GLMakie, NativeFileDialog

fig = Figure(size=(200, 200), backgroundcolor=RGBf(0.7, 0.8, 1))
btn_RUN = Button(fig, label = " Open file... ")
on(btn_RUN.clicks) do c;
    println("opening file dialog...")
    filename = fetch(Threads.@spawn pick_file(""))
    println(filename)
end
fig

```

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [March 25, 2024, 12:14pm UTC](https://discourse.julialang.org/t/how-to-avoid-julia-is-not-responding-when-using-a-file-dialog/112075/6 "2024-03-25T12:14:47Z")

</div>

OK, this seams to work and is easier than the first solution:

```julia
using GLMakie, NativeFileDialog

fig = Figure(size=(200, 200), backgroundcolor=RGBf(0.7, 0.8, 1))
btn_OPEN = Button(fig, label = " Open file... ")
on(btn_OPEN.clicks) do c;
    @async begin 
        filename = fetch(Threads.@spawn pick_file(""))
        println(filename)
    end
end
fig

```
