2014-11-14 50 views
0

希望你能帮助我这个one.i初学者在与C#的多线程编程。使用多线程写入文本文件使用C#

我正在尝试构建一个程序,使用两个线程在两个文本文件中编写范围为1到2000的所有数字。

每个线程都应该写入从1到2000的数字,这两个文件中没有任何一个找不到“文件中没有重复的数字”,并且每个线程都不应该写入另一个线程写入的数字。

在结束时,如果我们合并这两个文件的数字,我们应该从1号到2000年

这里是源代码我试图但是有一个问题,在写作的循环在下面图像

我不能由两个同步的线程处理写的过程中,我不得不exception

对象同步方法是从代码不同步块调用。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
using System.Threading; 

namespace Multithreading 
{ 
    class Program 
    { 

     static TextWriter file2 = new StreamWriter("file2 location"); 
     static TextWriter file1 = new StreamWriter("file1 location"); 

     static void Main(string[] args) 
     { 
      try 
      { 
       int[] array = new int[2000]; 
       Thread thread1 = new Thread(Program.writeinfile1); 
       Thread thread2 = new Thread(Program.writeinfile2); 

       for (int counter = 1; counter <= 2000; counter++) 
       { 
        thread1.Start(counter); 
        thread2.Start(++counter); 
        Monitor.Enter(thread1); 
        Monitor.Wait(thread1); 
        Monitor.PulseAll(thread2); 
       } 
      } 
      catch (FileNotFoundException) 
      { 
       Console.WriteLine("the file you are trying to open is not found"); 
      } 
     } 

     public static void writeinfile1(object x) 
     { 
      int converttointx = (int)x; 
      file1.WriteLine(converttointx); 
      file1.Close(); 
     } 

     public static void writeinfile2(object y) 
     { 
      int converttointy = (int)y; 
      file2.WriteLine(converttointy); 
      file2.Close(); 
     } 
    } 
} 
+0

我认为这是家庭作业吗?对你应该如何处理它有什么要求?因为如果没有,它就像循环一样简单,并在每个循环中添加2并写入结果。 –

+0

究竟是什么问题?你得到的错误是什么;什么时候突破? –

+0

结果已写入文件,但它只写入每个文件中的一个整数,但我不知道完成整个循环的问题是什么 –

回答

1

这里的多线程调用互相交谈的一个例子,以确保不重复的工作。 我没有完全按照你的要求去做,因为这看起来很有家庭作业。但希望这将帮助您解决找出您的问题...

using System; 
using System.Collections.Generic; 
using System.Threading; 
using System.Threading.Tasks; 

namespace StackOverflow 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      new Program(); 
      Console.WriteLine("done"); 
      Console.ReadKey(); 
     } 
     Program() 
     { 
      int noThreads = 5; 
      int target = 2000; 
      StartThread(noThreads, target); 
     } 

     //kicks off our threads/waits for all threads to complete before returning 
     void StartThread(int noThreads, int target) 
     { 
      int id = noThreads--; 
      if (id > 0) 
      { 
       Doer doer = new Doer(id, target); 
       Thread t = new Thread(doer.Do); 
       t.Start(); 
       StartThread(noThreads,target); 
       t.Join(); 
      } 
     } 


    } 

    class Doer 
    { 
     static int marker = 0; 
     static readonly object syncLocker = new object(); 
     readonly int id; 
     readonly int target; 

     public Doer(int id, int target) 
     { 
      this.id = id; 
      this.target = target; 
     } 

     public void Do() 
     { 
      while (marker < this.target) 
      { 
       int i; 
       lock (syncLocker) 
       { 
        i = ++marker; 
       } 
       System.Console.WriteLine("{0:00}: {1:###0}", id, i); 
       //Thread.Sleep(RandomNo()); //uncomment this & code below if your threads are taking turns/behaving too predictably 
      } 
     } 


     /* 
     static readonly Random rnd = new Random(); 
     static readonly object rndSyncLocker = new object(); 
     public static int RandomNo() 
     { 
      lock (rndSyncLocker) 
      { 
       return rnd.Next(0, 1000); 
      } 
     } 
     */ 

    } 
} 
0

你没有正确使用Monitor类。对Monitor.PulseAll(thread2);的呼叫应该被称为within thread the thread which owns the lock,在这种情况下,该呼叫将在writeinfile1writeinfile2方法内。

这就是为什么你所得到的例外:

对象同步方法从代码不同步块调用。

有关正确的方式下面的StackOverflow问题使用Monitor.PulseAll(object)