在“计时器”错误是“System.Windows.Forms.Timer”和“System.Threading.Timer”之间不明确的引用 [英] Error on 'Timer' is an ambiguous reference between 'System.Windows.Forms.Timer' and 'System.Threading.Timer'

查看:1308
本文介绍了在“计时器”错误是“System.Windows.Forms.Timer”和“System.Threading.Timer”之间不明确的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经证明错误:

 
'定时'是'System.Windows.Forms.Timer和System.Threading.Timer$ B $之间不明确的引用b

当我增加了时钟码

 使用系统; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;使用System.Windows.Forms的
;使用的System.Net.Sockets
;
使用的System.Threading;
使用System.Security.Cryptography;


命名空间SocketClient
{

公共部分类SocketClient:表
{
System.Net.Sockets.TcpClient ClientSocket的=新System.Net.Sockets.TcpClient();
的NetworkStream serverStream =默认(的NetworkStream);
字符串READDATA = NULL;


公共SocketClient()
{
的InitializeComponent();
定时器定时器=新的Timer();
timer.Tick + =新的EventHandler(TimerOnTick);
timer.Interval = 1000;
timer.Start();
}



私人无效TimerOnTick(对象发件人,EventArgs EA)
{
的Invalidate();
}

保护覆盖无效的OnPaint(PaintEventArgs的豌豆)
{
的StringFormat strfmt =新的StringFormat();
strfmt.Alignment = StringAlignment.Far;
strfmt.LineAlignment = StringAlignment.Far;

pea.Graphics.DrawString(DateTime.Now.ToString(F),
字体,新SolidBrush(前景色),
ClientRectangle,strfmt);

}

私人无效的getMessage()
{
,而(真)
{
serverStream = clientSocket.GetStream( );
INT BUFFSIZE = 0;
字节[] = inStream中新的字节[10025]
BUFFSIZE = clientSocket.ReceiveBufferSize;
serverStream.Read(插播广告,0,BUFFSIZE);
串returndata = System.Text.Encoding.ASCII.GetString(插播广告);
READDATA =+ returndata;
MSG();
}
}


私人无效MSG()
{
如果(this.InvokeRequired)
this.Invoke(新MethodInvoker(MSG));
,否则
textDisplay.Text = textDisplay.Text + Environment.NewLine +>>中+ READDATA;
}


私人无效buttonConnect_Click(对象发件人,EventArgs五)
{


//显示如果消息没有输入进去。
如果(string.IsNullOrEmpty(textName.Text)|| string.IsNullOrEmpty(textPort.Text)|| string.IsNullOrEmpty(textIP.Text))
{
MessageBox.Show(请进入名,IP地址和放大器;端口号);
}
,否则
{
//连接到服务器,如果所有3输入为输入
READDATA =Conected到NYP服务器...;
MSG();

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

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

螺纹ctThread =新主题(的getMessage);
ctThread.Start();


}
}

私人无效buttonSend_Click(对象发件人,EventArgs五)
{
//显示味精盒如果没有服务器连接
如果(serverStream == NULL)
{
MessageBox.Show(请连接到服务器第一!);
的回报;
}

//发送文本
字节[] = outStream System.Text.Encoding.ASCII.GetBytes(textSend.Text +$);
serverStream.Write(outStream,0,outStream.Length);
serverStream.Flush();

//明文
textSend.Text =;

}

私人无效textDisplay_TextChanged(对象发件人,EventArgs五)
{
textDisplay.SelectionStart = textDisplay.Text.Length;
textDisplay.ScrollToCaret();
textDisplay.Refresh();
}

私人无效textSend_TextChanged(对象发件人,EventArgs五)
{
buttonSend.Enabled = string.IsNullOrEmpty(textSend.Text)!;
}
}
}


解决方案

有各种选项的位置:




  • 使用一个别名:

     使用UITimer = System.Windows.Forms.Timer; 


    ...
    UITimer定时器=新UITimer();


  • 使用完全合格的名称:

      System.Windows.Forms.Timer定时器=新System.Windows.Forms.Timer(); 


  • 使用一个命名空间的别名:

     使用的WinForms = System.Windows.Forms的; 
    ...
    的WinForms ::定时器定时器=新的WinForms ::定时器();




不过,我个人建议分拆的从网络代码的用户界面代码 - 此时它不可能是一个问题。



我还要指出,您目前从流中读取,而不考虑回报。价值考虑在内 - 这是一个糟糕的主意,因为你不知道多少的缓冲区实际上包含了新的数据。


i have error shown:

'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);
        }
    }
}

解决方案

There are various options here:

  • Use an alias:

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

  • Use the fully qualified name:

    System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
    

  • Use a namespace alias:

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

However, I would personally suggest splitting up the user interface code from the network code - at which point it's unlikely to be an issue.

I would also note that you're currently reading from the stream without taking the return value into account - that's a bad idea, as you don't know how much of the buffer actually contains new data.

这篇关于在“计时器”错误是“System.Windows.Forms.Timer”和“System.Threading.Timer”之间不明确的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆