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

Locking objects to avoid Multi-Threading errors not working

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

    Locking objects to avoid Multi-Threading errors not working

    Hello,

    I am coding to avoid some Multi-Threading errors using the code displayed in your help guide. I am accessig a file objects from several strategies at the same time but I still get Multi-Threading issues.

    The code I use is as simple as the one you show in your site:

    private object lockObj = new object();

    private void CreateFile()
    {
    lock (lockObj)
    {
    string fileName = @"C:\Folder\File.txt";
    FileInfo f_info = new FileInfo( fileName );

    if (f_info.Exists)
    f_info.Delete();

    mFile = f_info.CreateText();
    CloseLog();
    }
    }

    private void CloseLog()
    {
    if (mFile != null)
    {
    mFile.Flush();
    mFile.Close();
    mFile = null;
    }
    }

    Any help about this?

    Thanks

    #2
    Hello guillembm,

    Are you trying to access another thread with that threads dispatcher? (This is what multi-threading is about)


    For writing to a file, may I confirm you are following the StreamWriter example in the help guide?
    https://ninjatrader.com/support/help...o_write_to.htm

    I have a script that writes to file you may find useful.
    https://ninjatrader.com/support/foru...nce#post100192


    However, a text file is not the same as thread and the text file cannot be locked. Multiple applications or multiple threads cannot access the file at the same time. The example script writes to file and the closes the file so that another write can be made.

    You could choose to write to different files.

    If you are closing the file after its been written to, you can choose to use a try and catch and use a timer to try the write again after a few seconds if it fails the first time.
    Or you can make a static class that does all of the writing to that file that uses a queue that all of the other scripts can use to send the text to and have it do the writing.

    The logic will need to be custom logic. This thread will remain open for any community members that would like to assist.

    You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our business development follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Finally I managed to fix it. After upgrading to Windows 2016 server things are working smoother.

      It seems I have no more multi-threading issues. Cross my fingers

      Thanks

      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
      5 views
      0 likes
      Last Post usazencortex  
      Started by sidlercom80, 10-28-2023, 08:49 AM
      168 responses
      2,266 views
      0 likes
      Last Post sidlercom80  
      Started by Barry Milan, Yesterday, 10:35 PM
      3 responses
      13 views
      0 likes
      Last Post NinjaTrader_Manfred  
      Working...
      X