Threads for dummies

  • Thread starter Svend-Erik Kjær Madsen
  • Start date
S

Svend-Erik Kjær Madsen

Hi
Can somone please tell me howto handle threads in the code below
<code ex>
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:
<code ex >
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.
 
S

Svend-Erik Kjær Madsen

Svend-Erik Kjær Madsen said:
Hi
Can somone please tell me howto handle threads in the code below
<code ex>
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:
<code ex >
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]
 
R

Robert Klemme

Svend-Erik Kjær Madsen said:
Hi
Can somone please tell me howto handle threads in the code below
<code ex>
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:
<code ex >
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top