Advertisement
omegastripes

mproc_counters.vbs

Sep 17th, 2013
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. option explicit
  2. dim mproc
  3. set mproc = new multiprocess
  4. mproc.launch "starter", "run", ""
  5.  
  6. ' main programm section
  7.  
  8. class starter
  9.    
  10.     public sub run()
  11.         host.startproc "base", "run", ""
  12.         msgbox "OK to terminate", vbinformation, "starter"
  13.         host.free "counter"
  14.         wscript.sleep 1000
  15.         host.terminate "counter"
  16.         host.free "display"
  17.         wscript.sleep 1000
  18.         host.terminate "display"
  19.         host.free base
  20.         wscript.sleep 1000
  21.         host.terminate base
  22.         host.free host.id
  23.     end sub
  24.    
  25. end class
  26.  
  27. class base
  28.    
  29.     public max
  30.    
  31.     public sub run()
  32.         dim node, text, n
  33.         host.startproc "iewindow", "", "display"
  34.         for n = 0 to 11
  35.             host.createproc "counter", "run", ""
  36.         next
  37.         max = 100
  38.         host.start("counter")
  39.     end sub
  40.    
  41.     public sub pushmax
  42.         max = max + 100
  43.     end sub
  44.    
  45.     public sub counter_oninitialized(source)
  46.         dim node, r, g, b
  47.         randomize
  48.         set node = display.document.createelement("div")
  49.         r = int(rnd*256)
  50.         g = int(rnd*256)
  51.         b = int(rnd*256)
  52.         node.style.backgroundcolor = rgb(r, g, b)
  53.         if (.5 * r) + g + (.25 * b) > 191 then
  54.             node.style.color = "#000"
  55.         else
  56.             node.style.color = "#FFF"
  57.         end if
  58.         node.style.width = 0
  59.         node.style.height = 20
  60.         display.document.getelementbyid("testarea").appendchild(node)
  61.         node.appendchild(display.document.createtextnode("starting"))
  62.         set source.process.tag = node
  63.         set node = nothing
  64.     end sub
  65.    
  66. end class
  67.  
  68. class counter
  69.    
  70.     public tag
  71.    
  72.     public sub run()
  73.         dim c
  74.         c = 0
  75.         do
  76.             c = c + 1
  77.             do while c > ancestor.max
  78.                 ancestor.pushmax
  79.             loop
  80.             tag.innertext = c
  81.             tag.style.width = int(tag.parentnode.offsetwidth * c / ancestor.max)
  82.         loop until host.release
  83.         tag.innertext = "terminated"
  84.     end sub
  85.    
  86. end class
  87.  
  88. class iewindow
  89.    
  90.     public document
  91.    
  92.     private ieapplication, sweepout
  93.    
  94.     private sub class_initialize()
  95.         sweepout = false
  96.         set ieapplication = wscript.createobject("internetexplorer.application", "ieobj_")
  97.         with ieapplication
  98.             .menubar = false
  99.             .toolbar = false
  100.             .resizable = true
  101.             .statusbar = false
  102.             .addressbar = false
  103.             .visible = true
  104.             .navigate "about:blank"
  105.         end with
  106.         set document = ieapplication.document
  107.         document.write "<html><head><title>Output</title><style>*{font-family: 'courier new'; font-size: 10pt; color: #000; background-color: #CCC; }</style></head><body><div id='testarea'></div></body>"
  108.         host.assignhandler "ieobj_onquit", 0
  109.     end sub
  110.    
  111.     public sub ieobj_onquit
  112.         if not sweepout then host.free 0
  113.     end sub
  114.    
  115.     private sub class_terminate()
  116.         sweepout = true
  117.         on error resume next
  118.         do while typename(document) = "HTMLDocument"
  119.             ieapplication.quit
  120.             wscript.sleep 1
  121.         loop
  122.     end sub
  123.    
  124. end class
  125.  
  126. ' do not modify service class section
  127.  
  128. class multiprocess
  129.    
  130.     public primary, ancestor, parent, process, err
  131.     public names, execs, hosts
  132.     public id, aid, isprimary
  133.     public classname, methodname, aliasname
  134.     public found, lost, active
  135.     public state, permit, release
  136.     private container, signature, wshshell
  137.    
  138.     public sub launch(startclassname, startmethodname, startaliasname)
  139.         permit = false
  140.         release = false
  141.         executeglobal "dim scene, host, ancestor, process"
  142.         if not isempty(host) then exit sub
  143.         set host = me
  144.         executeglobal "set host.err = err"
  145.         executeglobal "function getroot: set getroot = me: end function"
  146.         set parent = getroot
  147.         isprimary = not wscript.arguments.named.exists("task")
  148.         if isprimary then
  149.             dim sample
  150.             state = 24
  151.             randomize
  152.             signature = ""
  153.             do
  154.                 signature = signature & hex(rnd * 16)
  155.             loop while len(signature) < 16
  156.             aid = empty
  157.             id = 0
  158.             found = 0
  159.             lost = 0
  160.             set wshshell = createobject("WScript.Shell")
  161.             set primary = host
  162.             set ancestor = nothing
  163.             set process = nothing
  164.             set scene = parent
  165.             set parent.ancestor = nothing
  166.             set parent.process = nothing
  167.             set hosts = createobject("Scripting.Dictionary")
  168.             set execs = createobject("Scripting.Dictionary")
  169.             set names = createobject("Scripting.Dictionary")
  170.             classname = empty
  171.             methodname = empty
  172.             aliasname = empty
  173.             set hosts(0) = host
  174.             set container = getobject("new:{C08AFD90-F2A1-11D1-8455-00A0C91F3880}")
  175.             container.putproperty signature, parent
  176.             startproc startclassname, startmethodname, startaliasname
  177.             on error resume next
  178.             do
  179.                 for each sample in execs.keys
  180.                     if release or active = 0 then exit do
  181.                     if not (execs(sample) is nothing) then
  182.                         if execs(sample).status > 0 then
  183.                             abolish sample
  184.                         end if
  185.                     end if
  186.                     wscript.sleep 1
  187.                 next
  188.             loop
  189.             release = true
  190.             state = 28
  191.             scenequit
  192.         else
  193.             dim job
  194.             job = split(wscript.arguments.named("task"), ";")
  195.             signature = cstr(job(0))
  196.             do
  197.                 for each container in createobject("Shell.Application").windows
  198.                     if isobject(container.getproperty(signature)) then
  199.                         exit do
  200.                     end if
  201.                 next
  202.                 wscript.sleep 1
  203.             loop
  204.             aid = clng(job(1))
  205.             id = clng(job(2))
  206.             found = null
  207.             lost = null
  208.             set scene = container.getproperty(signature)
  209.             set primary = scene.host
  210.             set hosts = primary.hosts
  211.             set ancestor = hosts(aid)
  212.             if isresponsive(aid) then
  213.                 set parent.ancestor = ancestor.parent.process
  214.             else
  215.                 set parent.ancestor = nothing
  216.             end if
  217.             classname = cstr(job(3))
  218.             methodname = cstr(job(4))
  219.             aliasname = cstr(job(5))
  220.             state = 4
  221.             primary.implicate id, aliasname, host
  222.             executeglobal "set process = new " & classname
  223.             executeglobal "set host.process = process"
  224.             executeglobal "set scene." & aliasname & " = process"
  225.             if isresponsive(aid) then
  226.                 executeglobal "set host.ancestor.parent." & aliasname & " = process"
  227.             end if
  228.             state = 8
  229.             primary.staff host
  230.             ancestorevent "oninitialized"
  231.             state = 12
  232.             if methodname <> "" then
  233.                 do until permit
  234.                     wscript.sleep 10
  235.                 loop
  236.                 state = 16
  237.                 executeglobal "process." & methodname
  238.             end if
  239.             state = 20
  240.             ancestorevent "oncompleted"
  241.             state = 24
  242.             do until release
  243.                 wscript.sleep 10
  244.             loop
  245.             state = 28
  246.         end if
  247.     end sub
  248.    
  249.     public default function startproc(classname, methodname, aliasname)
  250.         set startproc = start(createproc(classname, methodname, aliasname))
  251.     end function
  252.    
  253.     public function createproc(classname, methodname, aliasname)
  254.         if aliasname = "" then aliasname = classname
  255.         newvar aliasname
  256.         scene.host.newvar aliasname
  257.         createproc = primary.spawn(id, classname, methodname, aliasname)
  258.     end function
  259.    
  260.     public function spawn(issuer, classname, methodname, aliasname)
  261.         if not release then
  262.             found = found + 1
  263.             spawn = found
  264.             active = found - lost
  265.             names(spawn) = aliasname
  266.             set hosts(spawn) = nothing
  267.             if not hosts.exists(aliasname) then
  268.                 hosts.add aliasname, createobject("Scripting.Dictionary")
  269.             end if
  270.             set hosts(aliasname)(spawn) = nothing
  271.             execs.add spawn, wshshell.exec("""" & wscript.fullname & """ """ & wscript.scriptfullname & """ ""/task:" & join(array(signature, issuer, spawn, classname, methodname, aliasname), ";") & """")
  272.         end if
  273.     end function
  274.    
  275.     public function start(target)
  276.         select case outline(target)
  277.         case "Nothing", "multiprocess"
  278.             do while getstate(target) < 12
  279.                 wscript.sleep 10
  280.             loop
  281.             if isresponsive(target) then
  282.                 set start = hosts(target).process
  283.                 hosts(target).permit = true
  284.             else
  285.                 set start = nothing
  286.             end if
  287.         case "Dictionary"
  288.             dim elt
  289.             set start = hosts(target)
  290.             for each elt in start.keys
  291.                 do while getstate(elt) < 12
  292.                     wscript.sleep 10
  293.                 loop
  294.             next
  295.             for each elt in start.keys
  296.                 if isresponsive(elt) then
  297.                     hosts(elt).permit = true
  298.                 end if
  299.             next
  300.         case else
  301.             set start = nothing
  302.         end select
  303.     end function
  304.    
  305.     public sub implicate(id, aliasname, host)
  306.         set hosts(aliasname)(id) = host
  307.         set hosts(id) = host
  308.     end sub
  309.    
  310.     public sub staff(host)
  311.         set hosts(host.process) = host
  312.     end sub
  313.    
  314.     public sub abolish(id)
  315.         if hosts.exists(names(id)) then
  316.             hosts(names(id))(id) = empty
  317.         end if
  318.         names(id) = empty
  319.         if isresponsive(id) then
  320.             hosts(hosts(id).process) = empty
  321.         end if
  322.         hosts(id) = empty
  323.         set execs(id) = nothing
  324.         lost = lost + 1
  325.         active = found - lost
  326.     end sub
  327.    
  328.     private sub ancestorevent(eventname)
  329.         if aid > 0 then
  330.             on error resume next
  331.             executeglobal "ancestor." & aliasname & "_" & eventname & " host.hosts(" & id & ")"
  332.             if err.number = 424 or err.number = 438 then err.clear
  333.         end if
  334.     end sub
  335.    
  336.     public sub assignhandler(handlername, byval varsqty)
  337.         dim vars
  338.         vars = ""
  339.         if varsqty > 0 then
  340.             do
  341.                 vars = vars & "param" & varsqty
  342.                 varsqty = varsqty - 1
  343.                 if varsqty = 0 then exit do
  344.                 vars = vars & ", "
  345.             loop
  346.         end if
  347.         executeglobal "sub " & handlername & "(" & vars & "): process." & handlername & " " & vars & ": end sub"
  348.     end sub
  349.    
  350.     public sub newvar(varname)
  351.         executecommand "dim " & varname
  352.     end sub
  353.    
  354.     public sub executecommand(command)
  355.         executeglobal command
  356.     end sub
  357.    
  358.     public function getstate(target)
  359.         select case outline(target)
  360.         case "multiprocess"
  361.             on error resume next
  362.             getstate = hosts(target).state
  363.             if err.number <> 0 then
  364.                 err.clear
  365.                 getstate = 64
  366.             end if
  367.         case "Nothing"
  368.             getstate = 1
  369.         case "Dictionary"
  370.             getstate = null
  371.         case empty
  372.             getstate = 0
  373.         case else
  374.             getstate = 64
  375.         end select
  376.     end function
  377.    
  378.     private function outline(target)
  379.         on error resume next
  380.         if hosts.exists(target) then
  381.             outline = typename(hosts(target))
  382.             if err.number <> 0 then
  383.                 err.clear
  384.                 outline = "Object"
  385.             end if
  386.         else
  387.             outline = empty
  388.         end if
  389.     end function
  390.    
  391.     public function isresponsive(target)
  392.         isresponsive = cbool(getstate(target) and 28)
  393.     end function
  394.    
  395.     public function getid(target)
  396.         on error resume next
  397.         if isobject(target) then
  398.             if isresponsive(target) then
  399.                 getid = hosts(target).id
  400.                 if err.number = 0 then exit function
  401.                 err.clear
  402.             end if
  403.         elseif primary.execs.exists(target) then
  404.             getid = target
  405.             exit function
  406.         end if
  407.         getid = null
  408.     end function
  409.    
  410.     public function gethost(target)
  411.         on error resume next
  412.         if hosts.exists(target) then
  413.             set gethost = hosts(target)
  414.             if err.number = 0 then exit function
  415.             err.clear
  416.         end if
  417.         set gethost = nothing
  418.     end function
  419.    
  420.     public sub free(target)
  421.         select case outline(target)
  422.         case "multiprocess"
  423.             on error resume next
  424.             gethost(target).release = true
  425.             err.clear
  426.         case "Dictionary"
  427.             dim elt
  428.             for each elt in gethost(target)
  429.                 free(elt)
  430.             next
  431.         end select
  432.     end sub
  433.    
  434.     public function joint(target, state, timeout)
  435.         dim reftime
  436.         reftime = timer
  437.         select case outline(target)
  438.         case "multiprocess", "Nothing"
  439.             do while getstate(target) < state
  440.                 if timeisout(timeout, reftime) then
  441.                     joint = false
  442.                     exit function
  443.                 end if
  444.                 wscript.sleep 10
  445.             loop
  446.         case "Dictionary"
  447.             dim elt
  448.             for each elt in gethost(target)
  449.                 do while getstate(elt) < state
  450.                     if timeisout(timeout, reftime) then
  451.                         joint = false
  452.                         exit function
  453.                     end if
  454.                     wscript.sleep 10
  455.                 loop
  456.             next
  457.         end select
  458.         joint = true
  459.     end function
  460.    
  461.     private function timeisout(timeout, reftime)
  462.         if timeout > 0 then
  463.             dim delta
  464.             delta = timer - reftime
  465.             if delta < 0 then delta = delta + 86400
  466.             if delta > timeout then
  467.                 timeisout = true
  468.             end if
  469.         else
  470.             timeisout = false
  471.         end if
  472.     end function
  473.    
  474.     public sub interrupt(target, timeout)
  475.         select case outline(target)
  476.         case "multiprocess"
  477.             on error resume next
  478.             with gethost(target).parent
  479.                 .wscript.timeout = timeout
  480.                 .wscript.quit
  481.             end with
  482.             err.clear
  483.         case "Dictionary"
  484.             dim elt
  485.             for each elt in gethost(target)
  486.                 interrupt elt, timeout
  487.             next
  488.         end select
  489.     end sub
  490.    
  491.     public sub kickout(target)
  492.         if primary.execs.exists(target) then
  493.             if getstate(target) < 64 then
  494.                 on error resume next
  495.                 primary.execs(target).terminate
  496.                 err.clear
  497.             end if
  498.         else
  499.             select case outline(target)
  500.             case "multiprocess"
  501.                 kickout getid(target)
  502.             case "Dictionary"
  503.                 dim elt
  504.                 for each elt in gethost(target)
  505.                     kickout(elt)
  506.                 next
  507.             end select
  508.         end if
  509.     end sub
  510.    
  511.     public sub terminate(target)
  512.         interrupt target, 1
  513.         if not joint(target, 64, 2) then kickout target
  514.     end sub
  515.    
  516.     public sub push(name, value)
  517.         container.putproperty name, value
  518.     end sub
  519.    
  520.     public function pop(name)
  521.         on error resume next
  522.         if isobject(container.getproperty(name)) then
  523.             set pop = container.getproperty(name)
  524.         else
  525.             pop = container.getproperty(name)
  526.         end if
  527.     end function
  528.    
  529.     private sub scenequit
  530.         if isprimary then
  531.             dim col, i, status
  532.             col = execs.keys
  533.             for i = ubound(col) to 0 step -1
  534.                 interrupt col(i), 1
  535.             next
  536.             wscript.sleep 2000
  537.             on error resume next
  538.             for i = ubound(col) to 0 step -1
  539.                 status = execs(col(i)).status
  540.                 if err.number = 0 and status = 0 then execs(col(i)).terminate
  541.                 err.clear
  542.             next
  543.             container.quit
  544.             createobject("wscript.shell").popup "scenequit", 1, "primary", 64
  545.         end if
  546.     end sub
  547.    
  548.     private sub class_terminate()
  549.         if state < 28 and isprimary then scenequit
  550.     end sub
  551.    
  552. end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement