提问者:小点点

C#如何解决异步任务问题?[副本]


我比较新,只是试着先面对。。。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace ProfiChat_Server
{
    public partial class Form1 : Form
    {

        static readonly object _lock = new object();
        static readonly Dictionary<int, TcpClient> list_clients = new Dictionary<int, TcpClient>();
        public Form1()
        {
            InitializeComponent();
        }

        private async void btnStart_Click(object sender, EventArgs e)
        {
            Task<int> task = ServerOpen();
            await task;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {

        }




        public async Task<int> ServerOpen()
        {
            return await Task.Factory.StartNew(() =>
            {
                int count = 1;

                string serverIP = txtServerIP.Text;
                int serverPort = Int32.Parse(txtServerPort.Text);

                TcpListener ServerSocket = new TcpListener(IPAddress.Parse(serverIP), serverPort);
                ServerSocket.Start();

                while (true)
                {
                    //TcpClient client = ServerSocket.AcceptTcpClient();
>>>>>>>>>>>>>>>>>   TcpClient client = await ServerSocket.AcceptTcpClientAsync();  <<<<<<<<ERROR<<<<<<<<<
                    lock (_lock) list_clients.Add(count, client);
                    Console.WriteLine("Someone connected!!");

                    Thread t = new Thread(handle_clients);
                    t.Start(count);
                    count++;
                }

            });
        }



        public static void handle_clients(object o)
        {
            int id = (int)o;
            TcpClient client;

            lock (_lock) client = list_clients[id];

            while (true)
            {
                NetworkStream stream = client.GetStream();
                byte[] buffer = new byte[1024];
                int byte_count = stream.Read(buffer, 0, buffer.Length);

                if (byte_count == 0)
                {
                    break;
                }

                string data = Encoding.ASCII.GetString(buffer, 0, byte_count);
                broadcast(data);
                Console.WriteLine(data);
                //var chatline = txtChat.Text;
                Form1 formObj = new Form1();
                formObj.txtChat.Text += data;
            }

            lock (_lock) list_clients.Remove(id);
            client.Client.Shutdown(SocketShutdown.Both);
            client.Close();
        }

        public static void broadcast(string data)
        {
            byte[] buffer = Encoding.ASCII.GetBytes(data + Environment.NewLine);

            lock (_lock)
            {
                foreach (TcpClient c in list_clients.Values)
                {
                    NetworkStream stream = c.GetStream();

                    stream.Write(buffer, 0, buffer.Length);
                }
            }
        }





    }
}

在第55行中(我用我得到错误CS4034:“await”运算符只能在异步lambda表达式中使用。。。。

我很难理解到底发生了什么,据我所知,异步“只是”用于在后台运行的任务,而不会阻塞程序的其余部分。

我感谢任何建议或帮助。上面的程序应该是一个tcp聊天客户端服务器。


共1个答案

匿名用户

每个异步函数必须返回“task”not void。

private async Task btnStart_Click(object sender, EventArgs e)
{
     Task<int> task = ServerOpen();
     await task;
}

相关问题