Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by odoumas · Jun 04, 2019 at 06:13 PM · windows 10native plugintiminginterop

Invoking Windows Multimedia timer results to hang

I am on Windows 10, Unity Editor 2018.3.12f1

I have a requirement for very fast (500 Hz) and precise updates, "independent" from the Unity main thread. I am targeting windows desktop only, so the Multimedia Timers seemed as a good (the only?) fit. However I witness some weird crashes.
When in Editor, when I press the play button, my sample game seems to be working correctly (ie prints in the Debug console, and with great precision when I used stopwatch to measure), but when i stop the game and try to run it again, the Editor hangs.

When i build and play, again everything seems normal (no debug messages there of course), but when i attempt to close the program via the 'X' button it hangs again.

I tried this library which also includes a working example, and I also created this very simple mono behaviour to test it but with same results:

 using UnityEngine;
 using System.Threading;
 using System.Runtime.InteropServices;
 using System;
 
 public class MultimediaTimer : MonoBehaviour
 {
     private UInt32 userCtx = 0;
    
     void Start()
     {
         // Run();
         Thread r = new Thread(() => Run());
         r.Start();
     }
 
     void Run()
     {
         MultimediaTimerCallback Callback = new MultimediaTimerCallback(TimerCallbackMethod);
         uint timerId = TimeSetEvent(100, 5, Callback, ref userCtx, 1);
         Thread.Sleep(1000);
         TimeKillEvent(timerId);
     }
 
     private static void TimerCallbackMethod(uint id, uint msg, ref uint userCtx, uint rsv1, uint rsv2)
     {
         Debug.Log("Hi");
     }
 
     private delegate void MultimediaTimerCallback(UInt32 id, UInt32 msg, ref UInt32 userCtx, UInt32 rsv1, UInt32 rsv2);
 
     [DllImport("winmm.dll", EntryPoint = "timeSetEvent")]
     private static extern UInt32 TimeSetEvent(UInt32 msDelay, UInt32 msResolution, MultimediaTimerCallback callback, ref UInt32 userCtx, UInt32 eventType);
 
     [DllImport("winmm.dll", EntryPoint = "timeKillEvent")]
     private static extern UInt32 TimeKillEvent(UInt32 uTimerId);
 }
 

My question is very simple. Is it possible to reliably call the multimedia timer from a Unity program? And if not could someone explain this behaviour I observe? I could encapsulate my timer logic in a separate process and use some form of IPC to achieve my requirement, but I would prefer for convenience to stay withing Unity. (Of course if you know any alternatives, by all means do say them)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image melanchall · Aug 08, 2019 at 12:43 PM 0
Share

The same problem.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tushar1985 · Jun 11, 2019 at 04:37 AM

Hi,

I am also facing same issue, did by any chance you got any solution.

Thanks is advance.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image odoumas · Oct 13, 2019 at 12:04 PM 0
Share

Unfortunately not. I assume (but I am not really sure) that the mm timer runs on it's own thread that is never killed when the game exists, and for some reason Unity doesn't like that and hangs. I guess a seperate process is one way to go, but I am not aware of any other way.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

109 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

functional multithreaded c++ code crashes if executed in Unity as native plugin 1 Answer

Loading Windows 10 DLL. "This operation is only valid in the context of an app container" 1 Answer

Unity Pro license stopped working after Windows 10 Update 0 Answers

how to share fonction beetwin Unity3D and visual studio (windows store; win10, xaml) 1 Answer

Can I use projects created on Windows editor in Linux editor? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges