# If expression within a thread

**URL:** <https://www.ruby-forum.com/t/if-expression-within-a-thread/145547>\
**Category:** Ruby\
**Created:** [August 11, 2008, 1:40pm UTC](https://www.ruby-forum.com/t/if-expression-within-a-thread/145547 "2008-08-11T13:40:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 11, 2008, 1:40pm UTC](https://www.ruby-forum.com/t/if-expression-within-a-thread/145547/1 "2008-08-11T13:40:44Z")

</div>

Hi,

I am using Watir and Autoit (win32 test tool)

Here is my code

```
  autoit = WIN32OLE.new("AutoItX3.Control")
  t = Thread.new(autoit) {
  if autoit.WinWait("Authentication Required", "", 5) ==

```

1  
autoit.Send(username)  
autoit.Send("{TAB}")  
autoit.Send(password)  
autoit.Send("{ENTER}")  
end  
}  
m = Thread.new(self) {self.goto(url)}  
m.join; t.join

When a user navigates to a url, an authentication dialog may appear.

If this appears I would like to enter a username and password

If I manually type the url and run this

```
 require 'win32ole'
 autoit = WIN32OLE.new("AutoItX3.Control")
 autoit.WinWait("Authentication Required", "", 5)

```

A success of 1 is returned.

I wonder if I can use if expressions in Ruby threads, as the if  
block is never executed. Maybe the winwait function in autoit is not  
able to thread.

Any ideas, please?

Thanks

Aidy

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 11, 2008, 4:38pm UTC](https://www.ruby-forum.com/t/if-expression-within-a-thread/145547/2 "2008-08-11T16:38:31Z")

</div>

Calls to windows API’s will block _all_ threads in Ruby. This is why  
Watir’s  
click\_no\_wait method spawns a process and not merely a thread.

Bret

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 12, 2008, 1:15pm UTC](https://www.ruby-forum.com/t/if-expression-within-a-thread/145547/3 "2008-08-12T13:15:34Z")

</div>

Bret,

On Aug 11, 3:35 pm, Bret P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [Note: parts of this message were removed to make it a legal post.]
> 
> Calls to windows API’s will block _all_ threads in Ruby. This is why Watir’s  
> click\_no\_wait method spawns a process and not merely a thread.
> 
> Bret

Thanks for getting back

```
  <code>
  autoit = WIN32OLE.new("AutoItX3.Control")
  m = Thread.new(self) {self.goto(url)}
  t = Thread.new(autoit) do
    sleep 3
    autoit.Send(username)
    autoit.Send("{TAB}")
    autoit.Send(password)
    autoit.Send("{ENTER}")
  end
  m.join; t.join
 </code>

```

The ‘goto’ method waits for the page to load.

If winole blocks threads in Ruby then the autoit thread would not get  
executed as there is an indefinite wait in the ‘goto’ method.

Aidy

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 12, 2008, 2:45pm UTC](https://www.ruby-forum.com/t/if-expression-within-a-thread/145547/4 "2008-08-12T14:45:51Z")

</div>

Bret,

On Aug 12, 12:13 pm, aidy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Aug 11, 3:35 pm, Bret P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > [Note: parts of this message were removed to make it a legal post.]
> 
> > Calls to windows API’s will block _all_ threads in Ruby. This is why Watir’s  
> > click\_no\_wait method spawns a process and not merely a thread.
> 
> > Bret

Sorry for cross posting but I spawned this thread on Watir general as  
well

Hi,

`
    autoit = WIN32OLE.new("AutoItX3.Control")
    Thread.new(autoit) do
      sleep 1
      autoit.WinWait("Authentication Required", "")
      autoit.Send(username)
      autoit.Send("{TAB}")
      autoit.Send(password)
      autoit.Send("{ENTER}")
     end
     self.goto(url)
   
I think autoit.WinWait executes faster than browser.goto => a hang. We
need a sleep even if it is only for 0.1 secs
I don’t think we need to explicitly create another thread for
self.goto as it is already in one.
Aidy`
