我创建了两个用于两个文件编写和文件读取的线程。通过使用同步,只有一个线程应该访问文件写入或文件读取。 [英] I had created two threads for two methds filewritting and filereading. By using synchronization only one thread should access either filewritting or filereading.

查看:53
本文介绍了我创建了两个用于两个文件编写和文件读取的线程。通过使用同步,只有一个线程应该访问文件写入或文件读取。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建一个文件时,它显示输出为文件创建成功并显示消息..我只想要文件写入或文件读取。



< b>我的尝试:



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:

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");
               }

推荐答案

你有问题吗?

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.

通过确保两个线程是互斥的,你就破坏了线程的目的。感兴趣的是什么?

只需制作2个子程序并依次调用它们就能完成这项工作。

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.


这篇关于我创建了两个用于两个文件编写和文件读取的线程。通过使用同步,只有一个线程应该访问文件写入或文件读取。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆