Search This Blog

Monday, August 25, 2008

File Read

using System;
using System.IO;

class FileRead

{

string filereadbuf; // buffer to store the content of file

public void ReadFile(string FileName, int FileSize)

{

char[] buf = new char[FileSize];

// lets define an array of type char field (i.e. variable) buf // for more help please see .net sdk StreamReader sr = new StreamReader(new FileStream(FileName, FileMode.Open, FileAccess.Read));

int retval = sr.ReadBlock(buf, 0, FileSize);

// no. of bytes read

Console.Write ("Total Bytes Read = " + retval + "\n");

filereadbuf = new string(buf);

// store it in our field

Console.WriteLine (filereadbuf);

// lets print on screen

sr.Close();

}

}

Threading in .net

void DisplayThread1()
{
while (_stopThreads == false)
{
// lock on the current instance of the class for thread #1
lock (this)
{
Console.WriteLine("Display Thread 1");
_threadOutput = "Hello Thread1";
Thread.Sleep(1000);
// simulate a lot of processing
// tell the user what thread we are in thread #1
Console.WriteLine("Thread 1 Output --> {0}", _threadOutput);
} // lock released for thread #1 here
}
}
/// /// Thread 1, Displays that we are in thread 1 (locked) ///
void DisplayThread2()
{
while (_stopThreads == false)
{
// lock on the current instance of the class for thread #2
lock (this)
{
Console.WriteLine("Display Thread 2");
_threadOutput = "Hello Thread2";
Thread.Sleep(1000);
// simulate a lot of processing
// tell the user what thread we are in thread #1
Console.WriteLine("Thread 2 Output --> {0}", _threadOutput);
} // lock released for thread #2 here
}
}