# Treads slowing down the process

**URL:** <https://www.ruby-forum.com/t/treads-slowing-down-the-process/141928>\
**Category:** wxRuby\
**Created:** [July 1, 2008, 9:04pm UTC](https://www.ruby-forum.com/t/treads-slowing-down-the-process/141928 "2008-07-01T21:04:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![joseph\_edelstein](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@joseph\_edelstein](https://www.ruby-forum.com/u/joseph_edelstein)\
**Post date:** [July 1, 2008, 9:04pm UTC](https://www.ruby-forum.com/t/treads-slowing-down-the-process/141928/1 "2008-07-01T21:04:35Z")

</div>

Hello,

I was having problems with my GUI freezing if you clicked off of it when  
it was running or if it was processing a large file. After researching  
the forum, I found a thread that dealt with the problem by adding a  
timer and Thread.pass to the App. This solved the problem of the GUI  
freezing or hanging.

However, it has caused the program to run very slowly. What use to take  
only a few seconds now takes a few minutes. People will be processing  
large text files with this program so speed is important.

Thanks in advance for any help.

Joe

Ruby code:

begin  
require ‘rubygems’  
rescue LoadError  
end

require ‘rubyscript2exe’  
require ‘wx’  
include Wx  
require ‘Bogus\_Frame.rb’  
require ‘bogus\_conts.rb’  
include Process

RUBYSCRIPT2EXE.lib = [“Bogus\_GUI.xrc”]  
RUBYSCRIPT2EXE.lib = [‘Bogus\_Frame.rb’]  
RUBYSCRIPT2EXE.lib = [‘bogus\_conts.rb’]

class BogusFrame \< BogusBase  
include Bogus\_code  
def initialize  
@ext\_def\_source = “.txt”  
@ext\_def\_result = “.txt”  
super  
evt\_button(source\_button) {|event| source(event)}  
evt\_button(result\_button) {|event| result(event)}  
evt\_button(start\_button) {|event| main}  
end#def initialize

def source(event)  
source\_path = file\_selector(  
“Select the file to load”,  
“”, “”,  
@ext\_def\_source,  
“CSV (_.csv)|_.csv|Plain text  
(_.txt)|_.txt|All files (_._)|_._”,  
CHANGE\_DIR,  
self  
)  
if source\_path == nil  
return nil  
else  
@source\_textbox.clear  
@source\_textbox.append\_text(source\_path)  
end  
@ext\_def\_source = source\_path[/[^.]\*$/]  
end#end def source

def result(event)  
result\_path = file\_selector(  
“Select the file to load”,  
“”, “”,  
@ext\_def\_result,  
“CSV (_.csv)|_.csv|Plain text  
(_.txt)|_.txt|All files (_._)|_._”,  
CHANGE\_DIR,  
self  
)  
if result\_path == nil  
return nil  
end  
# it is just a sample, would use SplitPath in real program  
@ext\_def\_result = result\_path[/[^.]\*$/]  
@result\_textbox.clear  
@result\_textbox.append\_text(result\_path)

end#end def source

end#end Class

class BogusApp \< App  
def on\_init()  
t = Wx::Timer.new(self, 55)  
evt\_timer(55) { Thread.pass }  
t.start(1)  
BogusFrame.new.show

end  
end

BogusApp.new.main\_loop()

---

<div class="post-metadata">

**Author:** ![joseph\_edelstein](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@joseph\_edelstein](https://www.ruby-forum.com/u/joseph_edelstein)\
**Post date:** [July 2, 2008, 8:12am UTC](https://www.ruby-forum.com/t/treads-slowing-down-the-process/141928/2 "2008-07-02T08:12:13Z")

</div>

Hello Joseph,

There is a certain problem with this. Threads tend to make things a bit  
slower with processing, due to the very nature of Threads. There is  
literally a pause current process, switch to next to give shared time,  
before switching back. So what may only take a few seconds, can  
increase  
when you involve Threads. The thing you have to remember, is that  
Threads  
are green in Ruby. Meaning they are software controlled, not Hardware  
controlled. As it is, what you are currently doing, to execute the  
Timer  
every millisecond, is about the best you can do, to increase the speed.  
So  
unfortunately, there’s not much else here that can be solved. Unless  
you  
wish to get into separating the code that processes the Text file, into  
a  
separate file, and using something like Open3 to call the second bit of  
code, and monitor the output from the sub-process.

That will be the only way you could truly get close-as-possible  
threading in  
Ruby.

On Tue, Jul 1, 2008 at 2:04 PM, Joseph E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
