Click here to Skip to main content
15,891,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
while I am creating a file it shows output as file created successfully and also displaying the message..I want only either file writing or file reading.

What I have tried:

C#
static void Main(string[] args)
        {
            demo Demo = new demo();
            Thread write = new Thread(new ThreadStart(Demo.FileWritting));
            write.Start();
            write.Join();
            Thread read = new Thread(new ThreadStart(Demo.FileReading));
            read.Start();
            read.Join();
        }
        
        public class demo
        {
            string path = @"D:/Demos.txt";
           public void FileWritting()
            {
               lock(this)
               {
                   if (!File.Exists(path)&&locking)
                   {
                       FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write);
                       StreamWriter sw = new StreamWriter(fs);
                       sw.WriteLine("Welcome to filehandling");
                       
                       sw.Close();
                       fs.Close();
                       Console.WriteLine("File creation of" + path + "is done successfully");
                   }
                   else if (File.Exists(path)&&locking==false)
                   {
                       Console.WriteLine("file already exists");
                   }
                      
               }
            }
           public static volatile bool locking = true;
           
            public void FileReading()
              {  
                   if (File.Exists(path)&&locking)
                   {
                       lock(this)
                       {
                       FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                       StreamReader sr = new StreamReader(fs);

                       string s = sr.ReadToEnd();

                       Console.WriteLine(s);
                       sr.Close();
                       fs.Close();
                       }
                      
                   }
                   else if(!File.Exists(path)&&locking==false)
                       Console.WriteLine("file doent exists");
               }
Posted
Updated 8-Nov-16 22:22pm
v3
Comments
Matt T Heffron 8-Nov-16 20:38pm    
It isn't clear what you are trying to accomplish.
As shown, there's no point in using Threads at all, since as soon as you start each thread, the Join() will block until the Thread completes. So this just could be all performed straight through in the Main().
The demo class has the locking flag which also accomplishes nothing since it is true and there's nothing that changes that.
Further, it is considered to be a very bad idea for a class instance to lock() itself. A better practice is for the class instance to have a private field of type object that is initialized to a dummy object to be used to lock the access to operations in the class.

Please use the "Improve question" above and clarify what you really are trying to do.
Member 12821006 9-Nov-16 1:59am    
for synchronization purpose I put lock their.

1 solution

Do you have a question ?
Quote:
I had created two threads for two methods filewritting and filereading. By using synchronization only one thread should access either filewritting or filereading.
By ensuring both threads are mutually exclusive, you defeat the purpose of threads. What is the interest ?
just making 2 sub-programs and calling them in turn would do the job.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900