gpt4 book ai didi

c# - 'Timer' 上的错误是 'System.Windows.Forms.Timer' 和 'System.Threading.Timer' 之间的模糊引用

转载 作者:太空狗 更新时间:2023-10-29 17:28:15 29 4
gpt4 key购买 nike

我有错误显示:

'Timer' is an ambiguous reference between 'System.Windows.Forms.Timer' and 'System.Threading.Timer' 
when i added the code for clock

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


namespace SocketClient
{

public partial class SocketClient : Form
{
System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
NetworkStream serverStream = default(NetworkStream);
string readData = null;


public SocketClient()
{
InitializeComponent();
Timer timer = new Timer();
timer.Tick += new EventHandler(TimerOnTick);
timer.Interval = 1000;
timer.Start();
}



private void TimerOnTick(object sender, EventArgs ea)
{
Invalidate();
}

protected override void OnPaint(PaintEventArgs pea)
{
StringFormat strfmt = new StringFormat();
strfmt.Alignment = StringAlignment.Far;
strfmt.LineAlignment = StringAlignment.Far;

pea.Graphics.DrawString(DateTime.Now.ToString("F"),
Font, new SolidBrush(ForeColor),
ClientRectangle, strfmt);

}

private void getMessage()
{
while (true)
{
serverStream = clientSocket.GetStream();
int buffSize = 0;
byte[] inStream = new byte[10025];
buffSize = clientSocket.ReceiveBufferSize;
serverStream.Read(inStream, 0, buffSize);
string returndata = System.Text.Encoding.ASCII.GetString(inStream);
readData = "" + returndata;
msg();
}
}


private void msg()
{
if (this.InvokeRequired)
this.Invoke(new MethodInvoker(msg));
else
textDisplay.Text = textDisplay.Text + Environment.NewLine + " >> " + readData;
}


private void buttonConnect_Click(object sender, EventArgs e)
{


// show the message if no input is enter.
if (string.IsNullOrEmpty(textName.Text) || string.IsNullOrEmpty(textPort.Text) || string.IsNullOrEmpty(textIP.Text))
{
MessageBox.Show("Please enter Name, IP Address & Port #");
}
else
{
//connect to the server if all 3 input is enter
readData = "Conected to NYP Server ...";
msg();

clientSocket.Connect(textIP.Text, Convert.ToInt32(textPort.Text));
serverStream = clientSocket.GetStream();

byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textName.Text + "$");
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();

Thread ctThread = new Thread(getMessage);
ctThread.Start();


}
}

private void buttonSend_Click(object sender, EventArgs e)
{
// Show msg box if no server is connected
if (serverStream == null)
{
MessageBox.Show("Please connect to a server first!");
return;
}

// Send text
byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textSend.Text + "$");
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();

// Clear text
textSend.Text = "";

}

private void textDisplay_TextChanged(object sender, EventArgs e)
{
textDisplay.SelectionStart = textDisplay.Text.Length;
textDisplay.ScrollToCaret();
textDisplay.Refresh();
}

private void textSend_TextChanged(object sender, EventArgs e)
{
buttonSend.Enabled = !string.IsNullOrEmpty(textSend.Text);
}
}
}

最佳答案

这里有多种选择:

  • 使用别名:

    using UITimer = System.Windows.Forms.Timer;

    ...
    UITimer timer = new UITimer();
  • 使用完全限定的名称:

    System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
  • 使用命名空间别名:

    using WinForms = System.Windows.Forms;
    ...
    WinForms::Timer timer = new WinForms::Timer();

但是,我个人建议将用户界面代码与网络代码分开 - 在这一点上不太可能成为问题。

我还要指出,您当前正在从流中读取数据而不考虑返回值 - 这是一个坏主意,因为您不知道缓冲区中有多少实际包含新数据。

关于c# - 'Timer' 上的错误是 'System.Windows.Forms.Timer' 和 'System.Threading.Timer' 之间的模糊引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2115606/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com