Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to run PlaySound() in separate thread?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to run PlaySound() in separate thread?

    Hello.

    While PlaySound() method playing sound - NT freezes.
    This works in NT7, but not work in NT8:

    PHP Code:
    OnBarUpdate()
    {
       Thread thr = new Thread(My_Play_Sound); 
       thr.Start((object)"long");
    }
    
    void My_Play_Sound(object dir)
    {
       PlaySound(Core.UserDataDir.ToString() + @"bin\Custom\Indicator\Sounds\"+     Instrument.MasterInstrument.Name +".wav");
       Thread.Sleep(600);
       PlaySound(Core.UserDataDir.ToString() + @"bin\Custom\Indicator\Sounds\"+     Range_Bar_Size +" "+ Bars.Period.Id.ToString() +".wav");
       Thread.Sleep(900);
       PlaySound(Core.UserDataDir.ToString() + @"bin\Custom\Indicator\Sounds\"+     "speed "+(string)dir +".wav");
    } 
    

    I tried naively variants
    PHP Code:
    TriggerCustomEvent( My_Play_Sound, (object)"long"); 
    
    and
    PHP Code:
    TriggerCustomEvent( Play_Sound, (object)@"C:\MySound.wav"); 
    
    but, they don't work too.


    What is correct approach?
    fx.practic
    NinjaTrader Ecosystem Vendor - fx.practic

    #2
    Hello fx.practic,

    Thanks for opening the thread.

    Creating a custom event would not tie your method to be run on a separate thread.

    If you want to create your own thread and add certain code to it, you will have to manage the thread resource within your script. I would not advise to create new threads on each OnBarUpdate() iteration. If you add a print to the My_Play_Sound() method, you will see this gets printed numerous times in the output window. You may also observe that the PlaySound()'s are getting hit but they are end up "suspended" after too many instances.

    Providing direction for using threads dives more into the realm of pure C# and begins to escape the scope of support that we provide. That said, I have made some modifications to your code to provide further direction.

    Here is a link to the NinjaScript LifeCycle and OnStateChange() that provide further instruction on maintaining custom resources:

    NinjaScript LifeCycle - http://ninjatrader.com/support/helpG...fecycle_of.htm

    OnStateChange() - http://ninjatrader.com/support/helpG...tatechange.htm

    Please let me know if I may be of further assistance.
    Attached Files

    Comment


      #3
      Clear + brief = useful ))

      Thanks a lot!
      fx.practic
      NinjaTrader Ecosystem Vendor - fx.practic

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by CarlTrading, 05-11-2026, 05:56 AM
      0 responses
      63 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 05-10-2026, 08:12 PM
      0 responses
      35 views
      0 likes
      Last Post CarlTrading  
      Started by Hwop38, 05-04-2026, 07:02 PM
      0 responses
      198 views
      0 likes
      Last Post Hwop38
      by Hwop38
       
      Started by CaptainJack, 04-24-2026, 11:07 PM
      0 responses
      364 views
      0 likes
      Last Post CaptainJack  
      Started by Mindset, 04-21-2026, 06:46 AM
      0 responses
      283 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Working...
      X