我希望你能提供足够详细的问题描述。
概述: 我的任务是从TCP流中获取数据,并在GUI上显示数据,同时格式化数据并将其写入文件。我可以单独完成所有项目,但我无法将它们整合在一起。
在我的主程序中,我调用了tcp_listener函数。
MyTcpListener.start();
MyTcpListener 不断循环并读取数据。
public class MyTcpListener
{
//public string start(ref CQ.ConQueue init_concurrent_queue)
public static void start()
{
TcpListener server = null;
try
{
// Set the TcpListener on port 13000.
Int32 port = 13000;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
// TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port);
// Start listening for client requests.
server.Start();
....
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
Console.WriteLine("Received: {0}", data);
// !!! ADD DATA TO QUE HERE
Program.ConQueue
// Process the data sent by the client.
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
data.ToString();
// Send back a response.
stream.Write(msg, 0, msg.Length);
Console.WriteLine("Sent: {0}", data);
}
在上面的部分中,每当我获取到新数据时,我都会将其写入控制台。但是我需要做的是将其排队到一个并发队列中,以便我的其他类可以从中出队并写入文件。
我该如何做? 我应该在主线程中创建对象,并通过引用传递给其他线程 / 类吗?
还是有一种方法可以多次实例化对象,但在内存中只创建一个队列,所有对象都可以访问它?