Blog from Saravanan Arumugam

Let us talk about Technologies

About

I am a software developer working in Microsoft technologies. I am a Microsoft certified Technology Specialist on .Net Framework 3.5 Windows Communication Foundation Applications.

I use this site to publish research works and experiments done during my professional development attempts.

Advertisements

3 responses to “About

  1. Albert Praveen February 22, 2012 at 2:50 am

    Hi I Albert praveen.
    I saw your blog it was good and helpful.I am new to .NET. I have a doubt that can we access same file using multiple methods at same time.If suppose one method reading the file and if that method is busy go to other method.So both method should access the same file at same time. please help me to solve this problem.

    • Saravanan Arumugam February 22, 2012 at 12:29 pm

      Hi Praveen,
      Thanks for your comment. When you work with a file, it is considered to be a shared resource. However, reading a shared resource is most of the time allowed (except if the user explicitly locks its access to the other users). Well, coming to your question, if you want to have your file read by multiple users/applications/methods simultaneously, it is by default possible. Go through http://msdn.microsoft.com/en-us/library/db5x7c0d.aspx to more on reading files.
      But when you attempt to modify a file, the file would be locked which means the other uses would not have access to it.

      To explain this, I have written a code where the same file is read by 3 different threads simultaneously and 3 different threads attempting to write something in it.
      Here is the code
      using System;
      using System.Collections.Generic;
      using System.Configuration;
      using System.Linq;
      using System.Text;
      using System.IO;
      using System.Threading.Tasks;
      using System.Threading;

      namespace MyNamespace
      {
      class Program
      {
      static void Main(string[] args)
      {

      Task[] readTasks =
      {
      Task.Factory.StartNew(()=>ReadFile()),
      Task.Factory.StartNew(()=>ReadFile()),
      Task.Factory.StartNew(()=>ReadFile())
      };

      Task[] writeTasks =
      {
      Task.Factory.StartNew(()=>WriteFile()),
      Task.Factory.StartNew(()=>WriteFile()),
      Task.Factory.StartNew(()=>WriteFile())
      };

      Console.WriteLine("Press any key to continue...");
      Console.ReadKey();
      }

      private static void ReadFile()
      {
      using (StreamReader reader = new StreamReader(@"C:\Temp\tempFile.txt"))
      {
      string value;
      while ((value = reader.ReadLine()) != null)
      {
      Console.WriteLine("From Thread:{0} - Line from the file: {1}",
      Thread.CurrentThread.ManagedThreadId, value);
      Thread.Sleep(500);
      }
      }
      }

      private static void WriteFile()
      {
      try
      {
      using (StreamWriter writer = new StreamWriter(@"C:\Temp\writeableTempFile.txt", true))
      {
      for (char c = 'a'; c <= 'z'; c++)
      {
      writer.WriteLine(c);
      Thread.Sleep(500);
      }
      }
      Console.WriteLine("File written successfully from Thread {0}", Thread.CurrentThread.ManagedThreadId);
      }
      catch (IOException ioEx)
      {
      Console.WriteLine(ioEx.Message + "Thread:" + Thread.CurrentThread.ManagedThreadId);
      }
      }
      }
      }

      Here is the output. Note that writing attempt failed and reading was successful even if it is simultaneous.
      From Thread:6 - Line from the file: 1
      Press any key to continue...
      From Thread:10 - Line from the file: 1
      From Thread:12 - Line from the file: 1
      From Thread:6 - Line from the file: 2
      From Thread:10 - Line from the file: 2
      From Thread:12 - Line from the file: 2
      From Thread:6 - Line from the file: 3
      From Thread:10 - Line from the file: 3
      From Thread:12 - Line from the file: 3
      The process cannot access the file 'C:\Temp\writeableTempFile.txt' because it is
      being used by another process.Thread:13
      The process cannot access the file 'C:\Temp\writeableTempFile.txt' because it is
      being used by another process.Thread:13
      From Thread:6 - Line from the file: 4
      From Thread:12 - Line from the file: 4
      From Thread:10 - Line from the file: 4
      From Thread:6 - Line from the file: 5
      From Thread:10 - Line from the file: 5
      From Thread:12 - Line from the file: 5
      From Thread:6 - Line from the file: 6
      From Thread:12 - Line from the file: 6
      From Thread:10 - Line from the file: 6
      From Thread:6 - Line from the file: a
      From Thread:10 - Line from the file: a
      From Thread:12 - Line from the file: a
      From Thread:6 - Line from the file: b
      From Thread:10 - Line from the file: b
      From Thread:12 - Line from the file: b
      From Thread:6 - Line from the file: c
      From Thread:10 - Line from the file: c
      From Thread:12 - Line from the file: c
      From Thread:6 - Line from the file: d
      From Thread:10 - Line from the file: d
      From Thread:12 - Line from the file: d
      From Thread:6 - Line from the file: e
      From Thread:10 - Line from the file: e
      From Thread:12 - Line from the file: e
      From Thread:6 - Line from the file: f
      From Thread:10 - Line from the file: f
      From Thread:12 - Line from the file: f
      From Thread:6 - Line from the file: g
      From Thread:10 - Line from the file: g
      From Thread:12 - Line from the file: g
      From Thread:6 - Line from the file: hi
      From Thread:10 - Line from the file: hi
      From Thread:12 - Line from the file: hi
      From Thread:6 - Line from the file: jkl
      From Thread:10 - Line from the file: jkl
      From Thread:12 - Line from the file: jkl
      From Thread:6 - Line from the file: mnop
      From Thread:10 - Line from the file: mnop
      From Thread:12 - Line from the file: mnop
      From Thread:6 - Line from the file: qrstu
      From Thread:10 - Line from the file: qrstu
      From Thread:12 - Line from the file: qrstu
      From Thread:6 - Line from the file: vwxyz
      From Thread:10 - Line from the file: vwxyz
      From Thread:12 - Line from the file: vwxyz
      File written successfully from Thread 11

      One more suggestion: Being new to .Net you can make use of StackOverflow.Com to post you questions and get instant answers.

      • Albert Praveen February 22, 2012 at 6:23 pm

        Thanks you for your reply it helped me so much. I feel very happy now.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: