Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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:
    TriggerCustomEventMy_Play_Sound, (object)"long"); 
    and
    PHP Code:
    TriggerCustomEventPlay_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
    JimNinjaTrader Customer Service

    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 Aviram Y, Today, 05:29 AM
      0 responses
      1 view
      0 likes
      Last Post Aviram Y  
      Started by quantismo, 04-17-2024, 05:13 PM
      3 responses
      25 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by ScottWalsh, 04-16-2024, 04:29 PM
      7 responses
      34 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by cls71, Today, 04:45 AM
      0 responses
      6 views
      0 likes
      Last Post cls71
      by cls71
       
      Started by mjairg, 07-20-2023, 11:57 PM
      3 responses
      216 views
      1 like
      Last Post PaulMohn  
      Working...
      X