Module: Ruiby_threader

Included in:
Ruiby, Ruiby_gtk
Defined in:
lib/ruiby_gtk/ruiby_threader.rb

Overview

Creative Commons BY-SA : Regis d'Aubarede <[email protected]> LGPL

Instance Method Summary collapse

Instance Method Details

#after(n, &blk) ⇒ Object

as anim, but one shot, after some millisecs no threading: the bloc is evaluated by gtk mainloop in main thread context


81
82
83
84
85
86
# File 'lib/ruiby_gtk/ruiby_threader.rb', line 81

def after(n,&blk) 
  GLib::Timeout.add(n) { 
    blk.call rescue log("#{$!} :\n  #{$!.backtrace[0..3].join("\n   ")}") 
    false
  }
end

#anim(n, &blk) ⇒ Object


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ruiby_gtk/ruiby_threader.rb', line 60

def anim(n,&blk)
  @hTimer||={}
  $on=false
  px=0
  px=GLib::Timeout.add(n) do
    unless $on
      $on=true
      blk.call rescue log("#{$!} :\n  #{$!.backtrace.join("\n   ")}")
      $on=false
    else
      p "pass"
    end
    ret=@hTimer[px] 
    @hTimer.delete(px) unless ret
    ret
  end
  @hTimer[px]=true
  px
end

#init_threaderObject

implictly called by Ruiby window creator initialize multi thread engine


7
8
9
10
11
12
13
14
15
# File 'lib/ruiby_gtk/ruiby_threader.rb', line 7

def init_threader
  unless defined?($__mainthread__)
    $__mainthread__= Thread.current
    $__mainwindow__=self
    @is_main_window=true
  else
    @is_main_window=false
  end
end

#on_idle(&blk) ⇒ Object

shot peridicly a bloc parameter no threading: the bloc is evaluated by gtk mainloop in main thread context return handle of animation. can be stoped by delete(anim) // NOT WORK!, return a Numeric…


46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ruiby_gtk/ruiby_threader.rb', line 46

def on_idle(&blk)
  ii=0
  px=GLib::Idle.add() do 
    begin
      blk.call(ii)
      ii=(ii+1)%1000_000
      true
    rescue Exception => e
      $on_idle_ok=false
      log("#{$!} :\n  #{$!.backtrace.join("\n   ")}")
      false
    end
  end
end

#threader(per) ⇒ Object

must be created by application (in initialize, after super), active the tread engine for caller window. if several windows, last created is the winner : gtk_invoke will throw to last treaded() window!


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ruiby_gtk/ruiby_threader.rb', line 19

def threader(per)
  return unless $__mainwindow__==self
  @queue=Queue.new
  $__queue__=@queue
  ici=self
  GLib::Timeout.add(per) {
 now=Time.now
    while @queue.size>0 
       mess= @queue.pop
       if Array===mess
          win,mess=*mess
       else
        win=ici
       end
       win.instance_eval(&mess) rescue log("#{$!} :\n  #{$!.backtrace[0..3].join("\n   ")}") 
 if (Time.now.to_f-now.to_f)>1
   win.update 
   now=Time.now
 end
    end
       true
      }
end