Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8745669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:04:14+00:00 2026-06-13T12:04:14+00:00

I want to write some content into a local file and then represent it

  • 0

I want to write some content into a local file and then represent it in a textblock. I have two methods, CreateFile and Output, the first method uses WriteTextAsync method to write content into file, and the second method uses ReadTextAsync method to read the content. I called the two methods one by one, like

CreateFile(data);
Output(file);

file is a global variable, CreateFile method will write the “data” into file, and Output method output the content of it.
Unfortunately, it not always work, sometimes, I got exception which said “Object reference not set to an object”, after researching, I found sometimes, the file is null, I think it may caused by Output method is executed, but the file creating doesn’t complete. So if I add a breakpoint, it always works. Anyone can help me how to let the Output method execute after the file creating completed?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:04:15+00:00Added an answer on June 13, 2026 at 12:04 pm

    One of the reasons might be that the file has not been created yet, when the second method tries to read it:

    Diagram 1

    So, the second method have to wait for the first method to finish:

    Diagram 2

    There are several ways to achieve that.

    One of them would be using Task Class from Task Parallel Library and its Wait Method:

    var task = new Task(() => CreateFile(data));
    task.Wait();
    

    Another one, for example, ManualResetEvent Class:

    ManualResetEvent allows threads to communicate with each other by signaling. Typically, this communication concerns a task which one thread must complete before other threads can proceed.

    A few other related links:

    • A great article on Joseph Albahari’s website about multithreading in C#: http://www.albahari.com/threading/

    • C# Frequently Asked Questions: Using Async for File Access

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a top articles system. I want to filter some content
I want to write some tel numbers to excel file, some of them start
I want to write some configuration in web.config file, that will make my ASP.Net
I want to write some debugging output to the log to review it with
I want to write some games, but I don't have any game development experience.
I want to write some text directly to a file using Flex 3 /
I want to write some bogus text in a file (helloworld text in a
I have a PHP script which I want to output some text which I'm
I have some content that i have to render as a excel file in
I want to write some plugin to analysis Java source code. Which part of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.