Announcement

Collapse
No announcement yet.

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.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    646 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    367 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    107 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    569 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    573 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X