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:
    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 vantojo, Today, 01:39 PM
      0 responses
      16 views
      0 likes
      Last Post vantojo
      by vantojo
       
      Started by Rubens_2020, Today, 01:48 PM
      1 response
      10 views
      0 likes
      Last Post NinjaTrader_Kate  
      Started by kempotrader, Today, 01:34 PM
      1 response
      23 views
      0 likes
      Last Post NinjaTrader_ChrisL  
      Started by bc24fl, Today, 01:50 PM
      1 response
      11 views
      0 likes
      Last Post NinjaTrader_BrandonH  
      Started by bosajin, 01-21-2021, 09:15 AM
      7 responses
      61 views
      0 likes
      Last Post NinjaTrader_PaulH  
      Working...
      X