File: threads.rb

package info (click to toggle)
ruby-gnome2 0.12.0-2sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,820 kB
  • ctags: 7,421
  • sloc: ansic: 61,387; ruby: 17,307; makefile: 85; xml: 35; sql: 13
file content (74 lines) | stat: -rw-r--r-- 1,361 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/local/bin/ruby
=begin
  threads.rb - Ruby/GTK sample script.

  Copyright (c) 2003 Ruby-GNOME2 Project Team
  This program is licenced under the same licence as Ruby-GNOME2.

  $Id: threads.rb,v 1.2 2005/03/02 12:06:36 silicio Exp $
=end

require 'gtk2'
require 'thread'

Gtk.init

label = Gtk::Label.new

Thread.new {
  (0...1000).each { |cnt|
    p "A:" + cnt.to_s
    label.label = "A:" + cnt.to_s
    sleep(2)
  }
}

start_button = Gtk::Button.new("start")
stop_button = Gtk::Button.new("stop")

start_button.signal_connect("clicked") do
  start_button.sensitive=false
  @th = Thread.new {
    (0...10).each { |cnt|
      p "B:" + cnt.to_s
      label.label = "B:" + cnt.to_s
      sleep(2)
    }
    @th = nil
    start_button.sensitive=true
    start_button.grab_focus
    stop_button.sensitive=false
  }
  stop_button.sensitive=true
  stop_button.grab_focus
end
  
stop_button.signal_connect("clicked") do
  if @th
    @th.kill
    puts "killed"
    @th = nil
    start_button.sensitive=true
    start_button.grab_focus
    stop_button.sensitive=false
  end
end

stop_button.sensitive=false

box = Gtk::VBox.new.
  set_size_request(100,100)

box << label << start_button << stop_button

win = Gtk::Window.new << box

win.show_all.signal_connect("delete_event") do
  p "Exiting..."
  Gtk.main_quit 
  Thread.list.each {|t| t.kill }
end

Gtk.main