Is there any command statement to simulate pressing ctrl-c?

Is there any command statement to simulate pressing ctrl-c to abort the running program, I am designing a gui abort button, and I want it to press the button to stop the running program. Similar to the “interrupt()” function, note that this is not to exit julia, such as base.exit(0), because I can also click the run button to re-run the program.

If you use a third-party package to simulate the keyboard and press ctrl-c, it will abort the running program,you can introduce it

using GLMakie
fig = Figure()
btrun =Button(fig[1,1],label="run program",tellwidth=false)
on(btrun.clicks) do s;test();end
btexitprogram =Button(fig[1,2],label="exit program",tellwidth=false)
on(btexitprogram.clicks) do s;interrupt_callback();end
display(fig)

#function:

function interrupt_callback()
    throw(InterruptException())
end
function test()
    tstart=time()
    for i=1:100
        sleep(1)
        tend=time()-tstart
        println("time:$tend")
    end
end

It seems like Ctrl-C generates an “InterruptException”, so throwing one of those might do it :slight_smile:

julia> try
       sleep(10)
       catch e
       @show e
       end
^Ce = InterruptException()
InterruptException()

julia> try
       throw(InterruptException())
       catch e
       @show e
       end
e = InterruptException()
InterruptException()

I tested this function, it doesn’t work for gui button to abort the running program.


using GLMakie
fig = Figure()
btrun =Button(fig[1,1],label="run program",tellwidth=false)
on(btrun.clicks) do s;test();end
btexitprogram =Button(fig[1,2],label="exit program",tellwidth=false)
on(btexitprogram.clicks) do s;interrupt_callback();end
display(fig)

function interrupt_callback()
     throw(InterruptException())# exit julia program
end
function test()
     tstart=time()
     for i=1:100
         sleep(1)
         tend=time()-tstart
         println("time:$tend")
     end
end

Pressing the button produces this result:

time:29.30900001525879
time:30.325000047683716
Error in callback:
InterruptException:
Stacktrace: 

   [1] interrupt_callback()
     @Main e:\BaiduSyncdisk\jupyter_julia\GLMakIe_pro.jl:196
   [2] (::var"#74#75")(s::Int64)
     @ Main e:\BaiduSyncdisk\jupyter_julia\GLMakIe_pro.jl.jl:190
   [3] #invokelatest#2
     @ .\essentials.jl:816 [inlined]
   [4] invokelatest
     @ .\essentials.jl:813 [inlined]
   [5] notify
     @ C:\Users\Administrator\.julia\packages\Observables\PHGQ8\src\Observables.jl:169 [inlined]
   [6] setindex!(observable::Observable, val::Any)
     @ Observables C:\Users\Administrator\.julia\packages\Observables\PHGQ8\src\Observables.jl:86
   [7] (::Makie.var"#1649#1659"{Button, Observable{Symbol}})(#unused#::MouseEvent)
     @Makie C:\Users\Administrator\.julia\packages\Makie\DekzU\src\makielayout\blocks\button.jl:68
   [8] (::Makie.var"#1127#1128"{Makie.var"#1649#1659"{Button, Observable{Symbol}}})(event::MouseEvent)
     @Makie C:\Users\Administrator\.julia\packages\Makie\DekzU\src\makielayout\mousestatemachine.jl:86
   [9] #invokelatest#2
     @ .\essentials.jl:816 [inlined]
  [10] invokelatest
     @ .\essentials.jl:813 [inlined]
  [11] notify
     @ C:\Users\Administrator\.julia\packages\Observables\PHGQ8\src\Observables.jl:169 [inlined]
  [12] setindex!
     @ C:\Users\Administrator\.julia\packages\Observables\PHGQ8\src\Observables.jl:86 [inlined]
  [13] (::Makie.var"#1197#1199"{Scene, Base.RefValue{Bool}, Base.RefValue{Union{Nothing, Makie.Mouse.Button}}, Base.RefValue{Float64}, Base. RefValue{Float64}, Base.RefValue{Bool}, Base.RefValue{Bool}, Base.RefValue{Union{Nothing, Makie.Mouse.Button}}, Base.RefValue{Bool}, Base.RefValue{Point{2, Float32}}, Base.RefValue{Point{2, Float32}}, Base.RefValue{Makie.Mouse.Action}, Observable{MouseEvent}, Float64, Module})(event::Makie.MouseButtonEvent)
     @Makie C:\Users\Administrator\.julia\packages\Makie\DekzU\src\makielayout\mousestatemachine.jl:256
  [14] #invokelatest#2
     @ .\essentials.jl:816 [inlined]
  [15] invokelatest
     @ .\essentials.jl:813 [inlined]
  [16] notify
     @ C:\Users\Administrator\.julia\packages\Observables\PHGQ8\src\Observables.jl:169 [inlined]
  [17] setindex!
     @ C:\Users\Administrator\.julia\packages\Observables\PHGQ8\src\Observables.jl:86 [inlined]
  [18] (::GLMakie.var"#mousebuttons#151"{Observable{Makie.MouseButtonEvent}})(window::GLFW.Window, button::GLFW.MouseButton, action::GLFW.Action, mods::Int32 )
     @GLMakie C:\Users\Administrator\.julia\packages\GLMakie\pyNln\src\events.jl:102
  [19] _MouseButtonCallbackWrapper(window::GLFW.Window, button::GLFW.MouseButton, action::GLFW.Action, mods::Int32)
     @GLFW C:\Users\Administrator\.julia\packages\GLFW\BWxfF\src\callback.jl:43
  [20] PollEvents
     @ C:\Users\Administrator\.julia\packages\GLFW\BWxfF\src\glfw3.jl:620 [inlined]
  [21] pollevents(screen::GLMakie. Screen{GLFW. Window})
     @GLMakie C:\Users\Administrator\.julia\packages\GLMakie\pyNln\src\screen.jl:422
  [22] on_demand_renderloop(screen::GLMakie.Screen{GLFW.Window})
     @GLMakie C:\Users\Administrator\.julia\packages\GLMakie\pyNln\src\screen.jl:871
  [23] renderloop(screen::GLMakie. Screen{GLFW. Window})
     @GLMakie C:\Users\Administrator\.julia\packages\GLMakie\pyNln\src\screen.jl:897
  [24] (::GLMakie.var"#69#70"{GLMakie.Screen{GLFW.Window}})()
     @GLMakie.\task.jl:514
time:31.338000059127808
time:32.34500002861023
time:33.35700011253357

Yeah, it seems that interrupts the Makie renderloop instead of the callback to test, which I think is expected when running like this.

Based on your results I assumed Makie was spawning callbacks in a different thread than the GUI is running in, which would make sense and probably explain why the interrupt didn’t affect the test function.
But trying your code I can’t even click the exit button since the program is not responding, so maybe that isn’t the case…

Maybe try to simply have a global boolean variable that states whether you have clicked the stop button, and have a check for that in the test function?