Glen Holcomb wrote:
Is there an easy/clean way to start a new process in Windows that will
execute just a section of code? I've tried win32-process but it spawns
a new version of the entire script which doesn't help in my situation.
Try to use ruby threads. Example
...
I'm having an
issue where it appears that some combination of wx-ruby, win32ole, and the
garbage collector are causing my app to hang. I was hoping to execute the
win32ole code in a separate process in hopes of avoiding the hanging
problem.
My first thought was you could make your own fork-like functionality.
But after I cooked up an example, I realized that the simpler thing
might be to just extract the section of code in question to a separate
script, and launch that in a new process with `start
separate_script.rb input_data`
Just in case it's useful, here's my initial solution:
-Adam
--------
require 'win32ole'
Datafile = "fork.dump"
done = false
do_ole = ARGV.shift
data,result=0,0
def oleprocess data
excel = WIN32OLE.new("excel.application") #your ole app here!
workbook = excel.Workbooks.Add();
excel.Range("a1")['Value'] = data;
excel.Range("a2")['Value'] = "=a1*a1";
result = excel.Range("a2")['Value']
excel.ActiveWorkbook.Close(0);
excel.Quit();
result
end
if (do_ole == "-o")
p "in new process"
File.open(Datafile,"rb"){|fp|data = Marshal.load(fp)}
result = oleprocess(data)
File.open(Datafile,"wb"){|fp|Marshal.dump(result,fp)}
exit
else
data = 42 #initial_processing_here. data could be arbitrarily complex
File.open(Datafile,"wb"){|fp|Marshal.dump(data,fp)}
timestamp = File.mtime(Datafile)
p "launching new process"
system("start ruby #{__FILE__} -o")
until done
print '.'; sleep(0.1) #do gui stuff and other work here while you
are waiting...
if (File.mtime(Datafile) > timestamp) #other process is done
File.open(Datafile,"rb"){|fp|result = Marshal.load(fp)}
puts "\nsquare of #{data} is #{result}"
done = true
end
end
end