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 rtwave, 04-12-2024, 09:30 AM
    4 responses
    31 views
    0 likes
    Last Post rtwave
    by rtwave
     
    Started by yertle, Yesterday, 08:38 AM
    7 responses
    29 views
    0 likes
    Last Post yertle
    by yertle
     
    Started by bmartz, 03-12-2024, 06:12 AM
    2 responses
    22 views
    0 likes
    Last Post bmartz
    by bmartz
     
    Started by funk10101, Today, 12:02 AM
    0 responses
    7 views
    0 likes
    Last Post funk10101  
    Started by gravdigaz6, Yesterday, 11:40 PM
    1 response
    9 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Working...
    X