Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C使用多线程写入文本文件#_C#_Multithreading_File - Fatal编程技术网

C# 使用C使用多线程写入文本文件#

C# 使用C使用多线程写入文本文件#,c#,multithreading,file,C#,Multithreading,File,希望你能在这方面帮助我。我是C多线程编程的初学者 我正在尝试构建一个程序,使用两个线程将范围从1到2000的所有数字写入两个文本文件 每个线程都应该写入在两个文件中任何一个都找不到的从1到2000的数字“文件中没有重复的数字”,并且每个线程都不应该写入由另一个线程写入的数字 最后,如果我们合并了两个文件的编号,那么编号应该是1到2000 这是我正在尝试的源代码,但在下图中编写for循环时出现了一个问题 我无法处理两个同步线程的写入过程,我有: 从未同步的代码块调用了对象同步方法 使用系统; 使用

希望你能在这方面帮助我。我是C多线程编程的初学者

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

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

最后,如果我们合并了两个文件的编号,那么编号应该是1到2000

这是我正在尝试的源代码,但在下图中编写for循环时出现了一个问题

我无法处理两个同步线程的写入过程,我有:

从未同步的代码块调用了对象同步方法

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.IO;
使用系统线程;
命名空间多线程
{
班级计划
{
静态TextWriter file2=新的StreamWriter(“file2位置”);
静态TextWriter file1=新的StreamWriter(“file1位置”);
静态void Main(字符串[]参数)
{
尝试
{
int[]数组=新int[2000];
Thread thread1=新线程(Program.writeinfile1);
Thread thread2=新线程(Program.writeinfile2);

对于(int counter=1;counter,这里有一个多线程调用相互对话以确保它们不会重复工作的示例。 我没有完全按照你的要求去做,因为这看起来很像家庭作业;但希望这能帮助你找到解决问题的方法

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); 
            }
        }
        */

    }
}
使用系统;
使用System.Collections.Generic;
使用系统线程;
使用System.Threading.Tasks;
命名空间堆栈溢出
{
班级计划
{
静态void Main(字符串[]参数)
{
新程序();
控制台。写入线(“完成”);
Console.ReadKey();
}
程序()
{
int=5;
int目标=2000;
StartThread(noThreads,target);
}
//启动线程/等待所有线程完成后再返回
void StartThread(int-noThreads,int-target)
{
int id=noThreads--;
如果(id>0)
{
实干者=新实干者(id,目标);
线程t=新线程(doer.Do);
t、 Start();
StartThread(noThreads,target);
t、 Join();
}
}
}
阶级实践者
{
静态int标记=0;
静态只读对象syncLocker=新对象();
只读int-id;
只读int目标;
公共行为人(内部id,内部目标)
{
this.id=id;
this.target=目标;
}
公营部门
{
while(标记
您没有正确使用
监视器
类。应该调用对
监视器.pulsell(thread2);
的调用,在这种情况下,该调用将在
writeinfile1
writeinfile2
方法中

这就是为什么会出现异常:

从未同步的代码块调用了对象同步方法

有关使用监视器.pulsell(对象)
的正确方法,请参阅以下堆栈溢出问题:


我假设这是家庭作业?对您应该如何处理它有什么要求吗?因为如果没有,它实际上很简单,只需循环并在每个循环中添加2,然后写入结果。到底是什么问题?您得到的错误是什么;何时中断?结果已写入文件,但它只写入一个整数在每个文件中,但我不知道完成整个文件的问题是什么loop@MohamedAdelIsmail代码只启动两个线程一次(就在for循环之前),因此它们只写一次。您想要什么行为?您可以启动两个线程,每个线程只吐出1000个数字,一个从1开始,另一个从1001开始。或者,它们可以有一个共享标记来显示它们所写的内容,在每次输出另一个数字时锁定并更新它。
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); 
            }
        }
        */

    }
}