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

write async Task

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

    write async Task

    Hello to the forum,

    I want to store data asynchronously on the hard disk.
    For this I use the following code:

    Call in NT8:
    Code:
    Task t = Task.Run(() => { WriteAsyncNT8.WriteAsync(@"G:\mytest.txt", CurrentBar, newBar, CurrentBar, Time[0]); });
    Processing in a dll:
    Code:
    public async Task WriteAsync(string path, int Init, bool NewBar, int Value, DateTime datetime)
    {
    if (Init == 1) { File.Delete(path); }
    
    if (Init > 1 && NewBar)
    {
    StreamWriter sw;
    sw = File.AppendText(path);
    await sw.WriteAsync(Value.ToString() + "; " + datetime + ";" + Environment.NewLine);
    sw.Close();
    }
    }

    As a result I get corrupted data:
    Code:
    [COLOR=#c0392b][B]20; 10.01.2019 21:31:00;
    20; 10.01.2019 21:31:00;[/B]
    [B]22; 10.01.2019 21:33:00;
    22; 10.01.2019 21:33:00;[/B][/COLOR]
    24; 10.01.2019 21:35:00;
    25; 10.01.2019 21:36:00;
    26; 10.01.2019 21:37:00;
    27; 10.01.2019 21:38:00;
    28; 10.01.2019 21:39:00;
    29; 10.01.2019 21:40:00;
    30; 10.01.2019 21:41:00;
    31; 10.01.2019 21:42:00;
    32; 10.01.2019 21:43:00;
    33; 10.01.2019 21:44:00;
    34; 10.01.2019 21:45:00;
    35; 10.01.2019 21:46:00;
    36; 10.01.2019 21:47:00;
    37; 10.01.2019 21:48:00;
    38; 10.01.2019 21:49:00;
    39; 10.01.2019 21:50:00;
    40; 10.01.2019 21:51:00;
    41; 10.01.2019 21:52:00;
    42; 10.01.2019 21:53:00;
    43; 10.01.2019 21:54:00;
    44; 10.01.2019 21:55:00;
    45; 10.01.2019 21:56:00;
    46; 10.01.2019 21:57:00;
    47; 10.01.2019 21:58:00;
    The data is not only wrong at the beginning, but also within the write process for larger amounts of data.
    Code:
    251; 10.01.2019 17:50:00;
    [COLOR=#c0392b][B]253; 10.01.2019 17:52:00;
    253; 10.01.2019 17:52:00;[/B][/COLOR]
    254; 10.01.2019 17:53:00;
    255; 10.01.2019 17:54:00;
    Is my programming faulty or do I have to cache the data first and then write it?

    Thanks a lot for the support!
    user10

    #2
    Hello user10,

    Thanks for your post.

    Something you could do is save the text to a string in the data thread. Then once the text is saved to a string in the data thread, write that string to the file in the async thread.

    Let us know if we may assist further.
    Brandon H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by AttiM, 02-14-2024, 05:20 PM
    12 responses
    211 views
    0 likes
    Last Post DrakeiJosh  
    Started by cre8able, 02-11-2023, 05:43 PM
    3 responses
    236 views
    0 likes
    Last Post rhubear
    by rhubear
     
    Started by frslvr, 04-11-2024, 07:26 AM
    8 responses
    114 views
    1 like
    Last Post NinjaTrader_BrandonH  
    Started by stafe, 04-15-2024, 08:34 PM
    10 responses
    47 views
    0 likes
    Last Post stafe
    by stafe
     
    Started by rocketman7, Today, 09:41 AM
    3 responses
    11 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Working...
    X