Threads for dummies


(Svend-Erik Kjær Madsen) #1

Hi
Can somone please tell me howto handle threads in the code below

def start_app
system(“echo #{$passwd}|sudo -S #{$appl}”}
$passwd.value = ‘’
$appl.value = ''
end
</code ex>
Works fine, but the system(…) thing, grabs focus ontil the application
stops.

So far Ive tried to write something like:

def start_app
Thread.new($passwd, $appl) do
t = Thread.current
passwd = t[:$passwd]
appl = t[:$appl]
system(“echo #{passwd}|sudo -S #{appl}”}
end
$passwd.value = ‘’
$appl.value = ''
end
</code ex>

This dosnt work

What I want is to create a subprocess running the programm, and return to
reset the variables.

···


/Sv-e


(Svend-Erik Kjær Madsen) #2

Svend-Erik Kjær Madsen wrote:

Hi
Can somone please tell me howto handle threads in the code below

def start_app
system(“echo #{$passwd}|sudo -S #{$appl}”}
$passwd.value = ‘’
$appl.value = ''
end
</code ex>
Works fine, but the system(…) thing, grabs focus ontil the application
stops.

So far Ive tried to write something like:

def start_app
Thread.new($passwd, $appl) do
t = Thread.current

Got this wrong

passwd = t[:$passwd]
appl = t[:$appl]
$passwd = t[:passwd]
$appl = t[:appl]

···

system(“echo #{passwd}|sudo -S #{appl}”}
end
$passwd.value = ‘’
$appl.value = ''
end
</code ex>

This dosnt work

What I want is to create a subprocess running the programm, and return to
reset the variables.


/Sv-e


(Robert) #3

“Svend-Erik Kjær Madsen” <sv-erik@fjernmig_stofanet.dk> schrieb im
Newsbeitrag news:40260d72$0$17636$ba624c82@nntp05.dk.telia.net

Hi
Can somone please tell me howto handle threads in the code below

def start_app
system(“echo #{$passwd}|sudo -S #{$appl}”}
$passwd.value = ‘’
$appl.value = ''
end
</code ex>
Works fine, but the system(…) thing, grabs focus ontil the
application
stops.

So far Ive tried to write something like:

def start_app
Thread.new($passwd, $appl) do
t = Thread.current
passwd = t[:$passwd]
appl = t[:$appl]
system(“echo #{passwd}|sudo -S #{appl}”}
end
$passwd.value = ‘’
$appl.value = ''
end
</code ex>

This dosnt work

What I want is to create a subprocess running the programm, and return
to
reset the variables.

It’s generally a bad idea to let the sub program reset the variables after
it is done because you can’t control the point in time and that might have
all kinds of strange effects. Better use local variables. Two
approaches:

def start_app(appl, passwd)
Thread.new do
system(“echo #{passwd}|sudo -S #{appl}”}
end
end

Or, if you don’t want to define a method for this, you should proceed like
this:

Thread.new($appl, $passwd) do |app, pass|
system(“echo #{pass}|sudo -S #{app}”}
end

This way “app” and “pass” are local variables and as soon as you started
the thread you can change values of those global variables to whatever you
like and they don’t interfere.

Regards

robert