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

  • Home
  • SEARCH
  • 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 8671507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:58:04+00:00 2026-06-12T18:58:04+00:00

public void fileReader() { Stream stream = new FileStream(filename, FileMode.Open , FileAccess.Read , FileShare.None,

  • 0
public void fileReader()
{
Stream stream = new FileStream(filename, FileMode.Open , FileAccess.Read , FileShare.None, 30, true);
byte[] Buffer = new byte[30];
while (stream.Read(Buffer, 0, 30) > 0)
{ 
 label1.text=Encoding.UTF8.GetString(Buffer); 
 Thread.Sleep(1000);
}
stream.Dispose();
}

THIS IS MY C# CODE TO READ 30 BYTES OF DATA AT A TIME FROM A FILE. I’ve created the Stream with FileStream constructor having useAsync=true. Here stream.Read method is used.

  1. Is this read operation working in ASYNCHRNOUS mode?

  2. How to check this?

  3. Another problem is that Encoding.UTF8.GetString(Buffer); gives white spaces as ‘□’ character… y?

  4. Is there any other way to make a 1 second delay other than Thread.Sleep(1000); ?

  • 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-12T18:58:05+00:00Added an answer on June 12, 2026 at 6:58 pm

    It doesn’t since you are using the syncronous Readmethod

    if you wish to read it asyncronous you would have to use the BeginRead method

    Below is an example on the usage of BeginRead from MSDN

    private void ReadStreamAsyncImpl(Stream stream)
        {
            chunk = new byte[chunkSize];
            stream.BeginRead(chunk, 
                             0, 
                             chunkSize, 
                             new AsyncCallback(BeginReadCallback), 
                             stream);
        }
    
        private void BeginReadCallback(IAsyncResult ar)
        {
            Stream stream = ar.AsyncState as Stream;
            int bytesRead = stream.EndRead(ar);
            StreamContentsAsString += StreamEncoding.GetString(chunk, 0, bytesRead);
    
            if (bytesRead < chunkSize) {
                // Finished
                isOperationInProgress = false;
                stream.Close();
                if (null != ReadStreamCompleted) {
                    ReadStreamCompleted(this, new EventArgs());
                }
            } else {
                ReadStreamAsyncImpl(stream);
            }
        }
    

    That said you should probably use a StreamReader to read the characters of the stream instead of converting them yourself.

    if you are using .NET 4.5 you can use ReadAsync as below (again from [MSDN][2])

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
    UnicodeEncoding uniencoding = new UnicodeEncoding();
    string filename = @”c:\Users\exampleuser\Documents\userinputlog.txt”;
    byte[] result;

            using (FileStream SourceStream = File.Open(filename, FileMode.Open))
            {
                result = new byte[SourceStream.Length];
                await SourceStream.ReadAsync(result, 0, (int)SourceStream.Length);
            }
    
            UserInput.Text = uniencoding.GetString(result);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public void GrabData() throws IOException { try { BufferedReader br = new BufferedReader(new FileReader(data/500.txt));
Code: button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // fileReader(); new xyz().execute(); }//
public void EditAndSave(String fileName) { Bitmap b = new Bitmap(fileName); /** * Edit bitmap
public void populateNotesFromFile() { try{ BufferedReader reader = new BufferedReader(new FileReader(DEFAULT_NOTES_SAVED)); String fileNotes =
import java.io.*; import java.util.Scanner; import java.util.StringTokenizer; public class Filereader { public static void main(String[]
public void openOptions() { Intent intent=new Intent(this,Game.class); //intent.putExtra(razmer, level); startActivity(intent); // Intent intent=new Intent(this,Options.class);
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Init(); backthread tr=new backthread(); tr.execute(0,0,0); } backthread
here is my code: public void mapTrace(String Path) throws FileNotFoundException, IOException { FileReader arq
I have this code: public void readTroops() { File file = new File(resources/objects/troops.txt); StringBuffer
I have to read a text and I created a method public void load(String

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.