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 NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      54 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      131 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      73 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      44 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      49 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X