• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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 Arkadiy_B, Today, 06:43 AM
      0 responses
      2 views
      0 likes
      Last Post Arkadiy_B  
      Started by leontancfa, Today, 06:09 AM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_PatrickG  
      Started by soulfx, 12-01-2017, 08:44 AM
      3 responses
      1,019 views
      0 likes
      Last Post NinjaTrader_EricB  
      Started by Parmenides48, 04-09-2019, 03:32 AM
      10 responses
      53 views
      0 likes
      Last Post NinjaTrader_PatrickG  
      Started by NinjaCustomer, Today, 01:48 AM
      1 response
      13 views
      0 likes
      Last Post NinjaTrader_EricB  
      Working...
      X