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);
}
}
}
This class provides methods to set the interval, and to start and stop the timer. Note. The Windows Forms Timer component is single-threaded, and is limited to an accuracy of 55 milliseconds. If you require a multithreaded timer with greater accuracy, use the Timer class in the System.
The application thread creates the timer, which waits one second and then executes the CheckStatus callback method every 250 milliseconds. The application thread then blocks until the AutoResetEvent object is signaled. When the CheckStatus callback method executes maxCount times, it calls the AutoResetEvent.
The Timer class generates an event after a set interval, with an option to generate recurring events. Firstly, create a timer object for 5 seconds interval − timer = new System.Timers.Timer(5000); Set elapsed event for the timer.
The problem is that you are
using System.Windows.Forms;
using System.Threading;
Both of these namespaces have a Timer
class and the compiler can't tell which one to use. When you declare your Timer
, use the full name, either:
System.Windows.Forms.Timer
or
System.Threading.Timer
WinForms Timer Class
Threading Timer Class
Based on your usage of the class, I think you want System.Windows.Forms.Timer
, like so:
public SocketClient()
{
InitializeComponent();
var timer = new System.Windows.Forms.Timer();
timer.Tick += new EventHandler(TimerOnTick);
timer.Interval = 1000;
timer.Start();
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With