Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Freezing host application (unity freezing) #33

Open
devingDev opened this issue May 23, 2024 · 2 comments
Open

Freezing host application (unity freezing) #33

devingDev opened this issue May 23, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@devingDev
Copy link

I'm on windows using Unity 2022.3.28f1 and the current latest commit on this repo.
I tried the nuget package and also this, but neither works perfectly inside Unity.

The second time I press play it freezes up and I have to force stop it in task manager.

Found this but i don't know how to fix it right now:
https://forum.zer7.com/topic/10126/

@devingDev
Copy link
Author

devingDev commented May 23, 2024

Fixed it this way if anyone cares:
In WinHidManager.cs

  1. Change this part in the Run method:
NativeMethods.MSG msg;
while (true)
{
    int result = NativeMethods.GetMessage(out msg, hwnd, 0, 0);
    if (result == 0 || result == -1) { break; }

    NativeMethods.TranslateMessage(ref msg);
    NativeMethods.DispatchMessage(ref msg);
}

to:

NativeMethods.MSG msg;
_hwnd = hwnd;
while (true)
{
    int result = NativeMethods.GetMessage(out msg, hwnd, 0, 0);
    if (result == 0 || result == -1) { break; }

    NativeMethods.TranslateMessage(ref msg);
    NativeMethods.DispatchMessage(ref msg);
}
  1. After or before the Run method add this:
		static IntPtr _hwnd;
		public static void QuitThisBs()
		{
			uint WM_QUIT = 0x0012;
			if (_hwnd != IntPtr.Zero)
			{ 
				PostMessage(_hwnd, WM_QUIT, IntPtr.Zero, IntPtr.Zero);
			}
		}

		[DllImport("user32.dll")]
		static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
  1. In your code where you exit or want to stop it call WinHidManager.QuitThisBs();
    (For me I did it in the OnDestroy method Unity provides, but you can call it anywhere even prematurely while game is still running)

  2. Done!

@benedekkupper benedekkupper added the bug Something isn't working label May 26, 2024
@benedekkupper
Copy link
Member

Thanks for your report and investigation on the fix! Care to open a PR with these changes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants