Announcement

Collapse
No announcement yet.

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 michelz, 02-18-2025, 08:30 AM
      39 responses
      1,118 views
      0 likes
      Last Post MiCe1999  
      Started by lacharp, 03-31-2025, 10:10 PM
      6 responses
      43 views
      0 likes
      Last Post richlink  
      Started by raysinred, Today, 10:32 AM
      0 responses
      8 views
      0 likes
      Last Post raysinred  
      Started by Majestic Rock Forum, 04-14-2025, 12:13 PM
      6 responses
      52 views
      0 likes
      Last Post Roonie23  
      Started by nagesh789, Today, 10:09 AM
      0 responses
      6 views
      0 likes
      Last Post nagesh789  
      Working...
      X