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 CortexZenUSA, Today, 12:53 AM
      0 responses
      1 view
      0 likes
      Last Post CortexZenUSA  
      Started by CortexZenUSA, Today, 12:46 AM
      0 responses
      1 view
      0 likes
      Last Post CortexZenUSA  
      Started by usazencortex, Today, 12:43 AM
      0 responses
      2 views
      0 likes
      Last Post usazencortex  
      Started by sidlercom80, 10-28-2023, 08:49 AM
      168 responses
      2,262 views
      0 likes
      Last Post sidlercom80  
      Started by Barry Milan, Yesterday, 10:35 PM
      3 responses
      11 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Working...
      X