C# 程序在此控制台中写入时在控制台中写入
当程序在此控制台中写入内容时,是否可以在控制台中写入内容?当您重命名或删除某些文件、执行重复操作以及程序在控制台中大量编写时,此功能非常有用。然后,当程序继续在控制台中写入时,您将能够编写一个命令来停止重复操作的执行。我认为这不是很清楚,我用我认为最合适的代码向您说明了这一事实(但我精确地说它不起作用;)。我们有三节课 主要类别:C# 程序在此控制台中写入时在控制台中写入,c#,multithreading,file-io,console,C#,Multithreading,File Io,Console,当程序在此控制台中写入内容时,是否可以在控制台中写入内容?当您重命名或删除某些文件、执行重复操作以及程序在控制台中大量编写时,此功能非常有用。然后,当程序继续在控制台中写入时,您将能够编写一个命令来停止重复操作的执行。我认为这不是很清楚,我用我认为最合适的代码向您说明了这一事实(但我精确地说它不起作用;)。我们有三节课 主要类别: using System; using System.Collections.Generic; using System.Linq; using System.Tex
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
private static bool m_Write;
public static bool write
{
get { return m_Write; }
set { m_Write = value; }
}
static void Main(string[] args)
{
int index = 0;
Console.ReadLine();
m_Write = true;
Reader reader = new Reader();
while (m_Write)
{
index++;
Writer writer = new Writer(index.ToString());
}
}
}
}
阅读课:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApplication1
{
class Reader
{
private Thread m_Reading_Thread;
private string m_text_To_Read;
public Reader()
{
m_Reading_Thread = new Thread(new ThreadStart(Read));
m_Reading_Thread.Start();
}
public void Read()
{
m_text_To_Read = Console.ReadLine();
if (m_text_To_Read == "Stop")
{
Program.write = false;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApplication1
{
class Writer
{
private Thread m_Writing_Thread;
private string m_Text_To_Write;
public Writer(string text_To_Write)
{
m_Text_To_Write = text_To_Write;
m_Writing_Thread = new Thread(new ThreadStart(Write));
m_Writing_Thread.Start();
}
public void Write()
{
Console.WriteLine(m_Text_To_Write);
}
}
}
写作课:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApplication1
{
class Reader
{
private Thread m_Reading_Thread;
private string m_text_To_Read;
public Reader()
{
m_Reading_Thread = new Thread(new ThreadStart(Read));
m_Reading_Thread.Start();
}
public void Read()
{
m_text_To_Read = Console.ReadLine();
if (m_text_To_Read == "Stop")
{
Program.write = false;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApplication1
{
class Writer
{
private Thread m_Writing_Thread;
private string m_Text_To_Write;
public Writer(string text_To_Write)
{
m_Text_To_Write = text_To_Write;
m_Writing_Thread = new Thread(new ThreadStart(Write));
m_Writing_Thread.Start();
}
public void Write()
{
Console.WriteLine(m_Text_To_Write);
}
}
}
类似这样的方法会奏效:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var w = new Writer();
var r = new Reader();
while (!r.finish)
{
w.enabled = true;
string k = Console.ReadKey(false).KeyChar.ToString();
w.enabled = false;
string line = k + Console.ReadLine();
r.Read(line);
}
}
}
class Writer
{
public bool enabled = true;
public Writer()
{
var timer = new System.Timers.Timer(1000);
timer.Elapsed += (a, b) =>
{
if(enabled)
Console.WriteLine("Test");
};
timer.Start();
}
}
class Reader
{
public bool finish = false;
public void Read(string line)
{
if (line == "stop")
{
finish = true;
}
}
}
}
不要担心,如果编写器在您键入的内容上面写入内容,Console.ReadLine()只会考虑您键入的内容。类似的操作可以:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var w = new Writer();
var r = new Reader();
while (!r.finish)
{
w.enabled = true;
string k = Console.ReadKey(false).KeyChar.ToString();
w.enabled = false;
string line = k + Console.ReadLine();
r.Read(line);
}
}
}
class Writer
{
public bool enabled = true;
public Writer()
{
var timer = new System.Timers.Timer(1000);
timer.Elapsed += (a, b) =>
{
if(enabled)
Console.WriteLine("Test");
};
timer.Start();
}
}
class Reader
{
public bool finish = false;
public void Read(string line)
{
if (line == "stop")
{
finish = true;
}
}
}
}
不要担心,如果编写器在您键入的内容上面写入内容,Console.ReadLine()只会考虑您键入的内容。类似的操作可以:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var w = new Writer();
var r = new Reader();
while (!r.finish)
{
w.enabled = true;
string k = Console.ReadKey(false).KeyChar.ToString();
w.enabled = false;
string line = k + Console.ReadLine();
r.Read(line);
}
}
}
class Writer
{
public bool enabled = true;
public Writer()
{
var timer = new System.Timers.Timer(1000);
timer.Elapsed += (a, b) =>
{
if(enabled)
Console.WriteLine("Test");
};
timer.Start();
}
}
class Reader
{
public bool finish = false;
public void Read(string line)
{
if (line == "stop")
{
finish = true;
}
}
}
}
不要担心,如果编写器在您键入的内容上面写入内容,Console.ReadLine()只会考虑您键入的内容。类似的操作可以:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var w = new Writer();
var r = new Reader();
while (!r.finish)
{
w.enabled = true;
string k = Console.ReadKey(false).KeyChar.ToString();
w.enabled = false;
string line = k + Console.ReadLine();
r.Read(line);
}
}
}
class Writer
{
public bool enabled = true;
public Writer()
{
var timer = new System.Timers.Timer(1000);
timer.Elapsed += (a, b) =>
{
if(enabled)
Console.WriteLine("Test");
};
timer.Start();
}
}
class Reader
{
public bool finish = false;
public void Read(string line)
{
if (line == "stop")
{
finish = true;
}
}
}
}
不要担心,如果编写器写入的内容超过了您正在键入的内容,Console.ReadLine()只会考虑您键入的内容。对于控制台应用程序,没有两个线程可以同时将数据写入屏幕
好的,在上面的回答中,Writes()的构造函数将持续执行,直到它完成运行。然后该控件将被传递给读卡器()。所以我认为这不适合你的需要。如果我错了,请更正。对于控制台应用程序,没有两个线程可以同时向屏幕写入数据
好的,在上面的回答中,Writes()的构造函数将持续执行,直到它完成运行。然后该控件将被传递给读卡器()。所以我认为这不适合你的需要。如果我错了,请更正。对于控制台应用程序,没有两个线程可以同时向屏幕写入数据
好的,在上面的回答中,Writes()的构造函数将持续执行,直到它完成运行。然后该控件将被传递给读卡器()。所以我认为这不适合你的需要。如果我错了,请更正。对于控制台应用程序,没有两个线程可以同时向屏幕写入数据
好的,在上面的回答中,Writes()的构造函数将持续执行,直到它完成运行。然后该控件将被传递给读卡器()。所以我认为这不适合你的需要。如果我错了,请纠正我。这并不像你想的那么复杂。通常有两种方法可以做到这一点。您可以启动一个后台线程来进行写入,并让控制台上的主线程块等待读取,也可以让主线程写入并让后台线程进行读取。我最喜欢第一种解决方案:
public class Program
{
private static readonly ManualResetEvent StopWriting = new ManualResetEvent(false);
private static void Main(string[] args)
{
Thread t = new Thread(WriterFunc);
t.Start();
string input;
do
{
input = Console.ReadLine();
} while (input != "stop");
// Tell the thread to stop writing
StopWriting.Set();
// And wait for the thread to exit
t.Join();
}
private static void WriterFunc()
{
int index = 0;
while (!StopWriting.WaitOne(Timeout.Infinite))
{
++index;
Console.WriteLine(index.ToString());
}
}
}
请注意,我在这里使用了ManualResetEvent
,而不是Boolean
标志。更好的解决方案是使用CancellationToken
。使用标志可能会导致各种有趣的问题,因为编译器可能会确定变量不能更改(它假定单线程访问)。即使在变量更改后,线程也可能继续运行
如果希望主线程执行写入操作,背景线程执行读取操作:
public class Program
{
private static readonly ManualResetEvent StopWriting = new ManualResetEvent(false);
private static void Main(string[] args)
{
Thread t = new Thread(ReaderFunc);
t.Start();
int index = 0;
while (!StopWriting.WaitOne(Timeout.Infinite))
{
++index;
Console.WriteLine(index.ToString());
}
// Wait for the background thread to exit
t.Join();
}
private static void ReaderFunc()
{
string input;
do
{
input = Console.ReadLine();
} while (input != "stop");
// Tell the main thread to stop writing
StopWriting.Set();
}
}
这并不像你想的那么复杂。通常有两种方法可以做到这一点。您可以启动一个后台线程来进行写入,并让控制台上的主线程块等待读取,也可以让主线程写入并让后台线程进行读取。我最喜欢第一种解决方案:
public class Program
{
private static readonly ManualResetEvent StopWriting = new ManualResetEvent(false);
private static void Main(string[] args)
{
Thread t = new Thread(WriterFunc);
t.Start();
string input;
do
{
input = Console.ReadLine();
} while (input != "stop");
// Tell the thread to stop writing
StopWriting.Set();
// And wait for the thread to exit
t.Join();
}
private static void WriterFunc()
{
int index = 0;
while (!StopWriting.WaitOne(Timeout.Infinite))
{
++index;
Console.WriteLine(index.ToString());
}
}
}
请注意,我在这里使用了ManualResetEvent
,而不是Boolean
标志。更好的解决方案是使用CancellationToken
。使用标志可能会导致各种有趣的问题,因为编译器可能会确定变量不能更改(它假定单线程访问)。即使在变量更改后,线程也可能继续运行
如果希望主线程执行写入操作,背景线程执行读取操作:
public class Program
{
private static readonly ManualResetEvent StopWriting = new ManualResetEvent(false);
private static void Main(string[] args)
{
Thread t = new Thread(ReaderFunc);
t.Start();
int index = 0;
while (!StopWriting.WaitOne(Timeout.Infinite))
{
++index;
Console.WriteLine(index.ToString());
}
// Wait for the background thread to exit
t.Join();
}
private static void ReaderFunc()
{
string input;
do
{
input = Console.ReadLine();
} while (input != "stop");
// Tell the main thread to stop writing
StopWriting.Set();
}
}
这并不像你想的那么复杂。通常有两种方法可以做到这一点。您可以启动一个后台线程来进行写入,并让控制台上的主线程块等待读取,也可以让主线程写入并让后台线程进行读取。我最喜欢第一种解决方案:
public class Program
{
private static readonly ManualResetEvent StopWriting = new ManualResetEvent(false);
private static void Main(string[] args)
{
Thread t = new Thread(WriterFunc);
t.Start();
string input;
do
{
input = Console.ReadLine();
} while (input != "stop");
// Tell the thread to stop writing
StopWriting.Set();
// And wait for the thread to exit
t.Join();
}
private static void WriterFunc()
{
int index = 0;
while (!StopWriting.WaitOne(Timeout.Infinite))
{
++index;
Console.WriteLine(index.ToString());
}
}
}
请注意,我在这里使用了ManualResetEvent
,而不是Boolean
标志。更好的解决方案是使用CancellationToken
。使用标志可能会导致各种有趣的问题,因为编译器可能会确定变量不能更改(它假定单线程访问)。即使在变量更改后,线程也可能继续运行
如果希望主线程执行写入操作,背景线程执行读取操作:
public class Program
{
private static readonly ManualResetEvent StopWriting = new ManualResetEvent(false);
private static void Main(string[] args)
{
Thread t = new Thread(ReaderFunc);
t.Start();
int index = 0;
while (!StopWriting.WaitOne(Timeout.Infinite))
{
++index;
Console.WriteLine(index.ToString());
}
// Wait for the background thread to exit
t.Join();
}
private static void ReaderFunc()
{
string input;
do
{
input = Console.ReadLine();
} while (input != "stop");
// Tell the main thread to stop writing
StopWriting.Set();
}
}
这并不像你想的那么复杂。通常有两种方法可以做到这一点。您可以启动一个后台线程来进行写入,并让控制台上的主线程块等待读取,也可以让主线程写入并让后台线程进行读取。我最喜欢第一种解决方案:
public class Program
{
private static readonly ManualResetEvent StopWriting = new ManualResetEvent(false);
private static void Main(string[] args)
{
Thread t = new Thread(WriterFunc);
t.Start();
string input;
do
{
input = Console.ReadLine();
} while (input != "stop");
// Tell the thread to stop writing
StopWriting.Set();
// And wait for the thread to exit
t.Join();
}
private static void WriterFunc()
{
int index = 0;
while (!StopWriting.WaitOne(Timeout.Infinite))
{
++index;
Console.WriteLine(index.ToString());
}
}
}
请注意,我在这里使用了ManualResetEvent
,而不是Boolean
标志。更好的解决方案是使用CancellationToken
。使用标志可能会导致各种有趣的问题,因为编译器可能会确定变量不能更改(它是