# Starting new process on Windows!

**URL:** <https://www.ruby-forum.com/t/starting-new-process-on-windows/145859>\
**Category:** Ruby\
**Created:** [August 14, 2008, 4:28pm UTC](https://www.ruby-forum.com/t/starting-new-process-on-windows/145859 "2008-08-14T16:28:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [August 14, 2008, 4:28pm UTC](https://www.ruby-forum.com/t/starting-new-process-on-windows/145859/1 "2008-08-14T16:28:21Z")

</div>

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.

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [August 14, 2008, 4:40pm UTC](https://www.ruby-forum.com/t/starting-new-process-on-windows/145859/2 "2008-08-14T16:40:58Z")

</div>

Glen H. 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.
> 
> –  
> “Hey brother Christian with your high and mighty errand, Your actions  
> speak  
> so loud, I can’t hear a word you’re saying.”
> 
> -Greg Graffin (Bad Religion)

Try to use ruby threads. Example

thread = Thread.new do  
Thread.stop #thread will not execute since you run it yourself  
p “Hi I’m thread”  
end

p “Before thread”  
thread.run  
p “After thread”

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [August 14, 2008, 4:49pm UTC](https://www.ruby-forum.com/t/starting-new-process-on-windows/145859/3 "2008-08-14T16:49:27Z")

</div>

On Thu, Aug 14, 2008 at 8:38 AM, Mateusz T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > 
> 
> ## thread.run p “After thread”
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Thanks, but I actually have the code in a thread already. 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.

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [August 14, 2008, 9:33pm UTC](https://www.ruby-forum.com/t/starting-new-process-on-windows/145859/4 "2008-08-14T21:33:22Z")

</div>

On Thu, Aug 14, 2008 at 12:24 PM, Adam S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > >
> 
> done = false  
> excel.Quit();  
> data = 42 #initial\_processing\_here. data could be arbitrarily complex  
> done = true  
> end  
> end  
> end

Thanks guys, I appreciate it but it would appear the problem only occurs  
on  
the Windows Server 2003 machine I was testing on. It works fine in XP  
which  
is a bit strange.

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [August 14, 2008, 8:28pm UTC](https://www.ruby-forum.com/t/starting-new-process-on-windows/145859/5 "2008-08-14T20:28:19Z")

</div>

On 8/14/08, Glen H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [August 15, 2008, 8:10pm UTC](https://www.ruby-forum.com/t/starting-new-process-on-windows/145859/6 "2008-08-15T20:10:09Z")

</div>

On 8/15/08, Glen H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Thu, Aug 14, 2008 at 1:27 PM, Glen H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> Actually the problem has re-surfaced. Basically what I need is fork  
> functionality. I have a small bit of code that I need to run as a separate  
> process. It would solve two problems if I could achieve this:

> I will probably have to resort to system although I’d rather not wait for  
> the code to finish execution as the results don’t really matter to the main  
> thread of execution, and it takes a while.

On Windows, use:  
system “start #{myapp}”

start spawns a new process then returns right away.

-Adam

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [August 15, 2008, 6:47pm UTC](https://www.ruby-forum.com/t/starting-new-process-on-windows/145859/7 "2008-08-15T18:47:33Z")

</div>

On Thu, Aug 14, 2008 at 1:27 PM, Glen H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > ## spawns the
> > 
> > excel.Range(“a2”)[‘Value’] = “=a1\*a1”;  
> > File.open(Datafile,“wb”){|fp|Marshal.dump(result,fp)}  
> > if (File.mtime(Datafile) \> timestamp) #other process is done  
> > which  
> > is a bit strange.
> 
> –  
> “Hey brother Christian with your high and mighty errand, Your actions speak  
> so loud, I can’t hear a word you’re saying.”
> 
> -Greg Graffin (Bad Religion)

Actually the problem has re-surfaced. Basically what I need is fork  
functionality. I have a small bit of code that I need to run as a  
separate  
process. It would solve two problems if I could achieve this:

1. The code is a bit slow as it uses win32ole so the app wouldn’t appear  
to  
stall
2. Separate heap/object space will hopefully fix the problem with the  
gui  
becoming totally unresponsive.

I will probably have to resort to system although I’d rather not wait  
for  
the code to finish execution as the results don’t really matter to the  
main  
thread of execution, and it takes a while.

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [August 15, 2008, 8:20pm UTC](https://www.ruby-forum.com/t/starting-new-process-on-windows/145859/8 "2008-08-15T20:20:08Z")

</div>

On Fri, Aug 15, 2008 at 12:06 PM, Adam S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > the code to finish execution as the results don’t really matter to the  
> > -Adam

Awesome!!! Thanks Adam.

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)
