schmich / win32-window

Ruby interface to the Win32 window management APIs.
https://rubygems.org/gems/win32-window
MIT License
4 stars 0 forks source link

Stuck if called in a thread #8

Open Andrek25 opened 4 years ago

Andrek25 commented 4 years ago

I don't know why but i think the problem is in the method get_title(handle) in GetWindowTextW library, when it's called 29 times it gets stuck.

Windows 7 (32 Bits)

Edit: I know that calling it 29 times is not the cause of the problem.

schmich commented 4 years ago

I'm on Windows 10 64-bit and I'm not able to reproduce this.

Can you create a small script to reproduce the behavior you're seeing?

The following snippet runs and terminates as expected:

require 'win32/window'

include Win32

Thread.new do
  100.times do
    Window.find.each do |w|
      puts w.title
    end
  end
end.join
Andrek25 commented 4 years ago

I examined the problem and found that the fault is because of another gem, Gosu, when I load an image with Gosu(2D game development library) into a class in a Thread the error occurs.

Example:

require 'win32/window'
require 'gosu'

class A
    Gosu::Image.new("image.bmp") # Comment this line to work correctly.
    def initialize
        puts Win32::Window.find(title: //i).size
    end
end

Thread.new { A.new }.join

I'm going to ask jlnr(creator of Gosu) about this bug, long ago, he told me that Gosu doesn't work with threads.

jlnr commented 4 years ago

The lack of thread safety in Gosu itself is mostly because I couldn't use the standard <mutex> header for a long time (it's now available on all platforms). As for GetWindowTextW, this forum thread explains how the function works, and why it can freeze in a multithreaded setup. Interesting, I'll probably run into the same issue if I ever try to make Gosu more thread-safe.

Andrek25 commented 4 years ago

This really helped me, I restructured all my code(i'm tired) based on this, thanks a lot :)