當程序在此控制檯中寫入內容時,是否可以在控制檯中寫入某些內容?當您重命名,刪除某些文件,執行重複操作時,以及程序在控制檯中寫入很多內容時,這會很有用。然後,當程序繼續在控制檯中寫入時,您將能夠編寫一條命令來停止執行重複動作。我認爲這不是很清楚,我用最合適的代碼向你說明了這一事實(但我確切地說它不起作用;))。我們有3個班。程序在此控制檯中寫入時在控制檯中寫入
主類:
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);
}
}
}
哇!有用 !非常感謝 !唯一的「問題」是程序寫的這個詞可以放在你正在編寫的工作中。你是否認爲有一種方法可以避免寫作者在上面輸入的內容上面寫? – Veriditas
我已經編輯在Writer類上創建一個防止寫入控制檯的標誌。該標誌在按鍵時激活,並在完成該行並按下「Enter」時釋放 – LcSalazar
再次編輯...現在它保存按下的觸發該標誌的按鍵。 – LcSalazar