Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > .NET Framework > Forum > [C#] การสั่งให้ทำงานใน Timer ผมอยากจะให้มันทำงานใน Timer จะเขียนได้ยังไงครับ


 

[.NET] [C#] การสั่งให้ทำงานใน Timer ผมอยากจะให้มันทำงานใน Timer จะเขียนได้ยังไงครับ

 
Topic : 123343



โพสกระทู้ ( 171 )
บทความ ( 0 )



สถานะออฟไลน์



ตอนนี้สามารถรับค่าจาก Arduino ได้แล้วครับ แต่ติดตรงที่ว่ามันต้องทำการกดปุ่ม ผมอยากจะให้มันทำงานใน Timer จะเขียนได้ยังไงครับ

Code (C#)
001.using System;
002.using System.Collections.Generic;
003.using System.ComponentModel;
004.using System.Data;
005.using System.Drawing;
006.using System.Linq;
007.using System.Net;
008.using System.Net.Sockets;
009.using System.Text;
010.using System.Threading;
011.using System.Windows.Forms;
012.using Utility;
013. 
014.namespace Wifi_Control
015.{
016.    public partial class Form1 : Form
017.    {
018.        private Socket client;
019.        private Thread receiver;
020.        private byte[] data = new byte[1024];
021.        private byte[] data1 = new byte[1024];
022.        public Form1()
023.        {
024.            InitializeComponent();
025.        }
026.        private void connected(IAsyncResult iar)
027.        {
028.            try
029.            {
030.                client.EndConnect(iar);
031. 
032.                lblsta.Text = "Connected";
033.                receiver = new Thread(new ThreadStart(ReceiveData));
034.                receiver.Start();
035.                 
036.            }
037. 
038.            catch
039.            {
040.                MessageBox.Show("ไม่สามารถเชื่อต่อได้ โปรดตรวจสอบข้อมูล");
041. 
042.            }
043. 
044.        }
045. 
046.        private void ReceiveData()  //รับ packet จาก Client/Server
047.        {
048.            int recv;
049.            string strData;
050. 
051. 
052.            while (client.Connected)
053.            {
054. 
055.                try
056.                {
057.                    recv = client.Receive(data); // คืนค่าเป็นความยาวของ Data
058.                    strData = Encoding.Default.GetString(data, 0, recv);
059.                    string[] getdata = strData.Split(',');
060.                    textBox1.Text = getdata[0];
061.                    textBox2.Text = getdata[1];
062. 
063.                    //richTextBox2.AppendText("รับ: " + strData + "\n");
064. 
065. 
066. 
067. 
068.                }
069.                catch (SocketException ex)
070.                {
071.                    MessageBox.Show(ex.Message, "Client");
072.                }
073.            }
074.             
075.        }   
076. 
077.        private void BtnCon_Click(object sender, EventArgs e)
078.        {
079.            try
080.            {
081.                client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
082.                IPEndPoint iep = new IPEndPoint(IPAddress.Parse(TxtIP.Text), int.Parse(TxtPort.Text));
083.                client.BeginConnect(iep, new AsyncCallback(connected), client);
084. 
085.            }
086.            catch
087.            {
088.                MessageBox.Show("เชื่อมต่อเซิฟเวอร์ไม่สำเร็จ โปรดตรวจความถูกต้อง");
089.            }
090.        }
091. 
092.        private void label2_Click(object sender, EventArgs e)
093.        {
094. 
095.        }
096.        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
097.        {
098.            reg["port"] = TxtPort.Text;
099.            reg["url"] = TxtIP.Text;
100.            try
101.            {
102.                BtnCon_Click(sender, e);
103.            }
104.            catch
105.            {
106.            }
107.        }
108.        string port = "";
109.        string url = "";
110.        MyReg reg = new MyReg("ArduinoAll");
111.        private void Form1_Load(object sender, EventArgs e)
112.        {
113. 
114.            timer1.Interval = 1000;
115.            timer1.Enabled = true;
116.            
117.            
118. 
119.            try
120.            {
121.                port = reg["port"].ToString();
122.                url = reg["url"].ToString();
123. 
124.                TxtIP.Text = url;
125.                TxtPort.Text = port;
126.            }
127.            catch
128.            {
129.                reg["port"] = "";
130.                reg["url"] = "";
131.            }
132. 
133.         
134.        }
135. 
136.        private void BtnDis_Click(object sender, EventArgs e)
137.        {
138.            if (client.Connected)
139.            // receiver.Abort();
140.                receiver.Abort(client);
141.                lblsta.Text = "Disconnected";
142.                client.Close(5);
143.            }
144.            else
145.            {
146.                MessageBox.Show("ไม่มีการเชื่อมต่อ");
147.            }
148.        }
149.        private void SendData(IAsyncResult iar)
150.        {
151.            try
152.            {
153.                Socket remote = (Socket)iar.AsyncState;
154.                int sent = remote.EndSend(iar);
155.            }
156.            catch (SocketException ex)
157.            {
158.                MessageBox.Show(ex.Message);
159.            }
160.        }
161.        private void button1_Click(object sender, EventArgs e)
162.        {
163.            InputTxt.Text = "ON";
164.            BtnSend_Click(sender, e);
165.        }
166. 
167.        private void BtnSend_Click(object sender, EventArgs e)
168.        {
169.            if (client.Connected)
170.            {
171.                byte[] input = Encoding.Default.GetBytes(InputTxt.Text);
172.                client.BeginSend(input, 0, input.Length, 0, new AsyncCallback(SendData), client);
173.                richTextBox1.AppendText("ส่ง: " + InputTxt.Text + "\n");
174.            }
175.            else
176.            {
177. 
178.                MessageBox.Show("ไม่สามารถเชื่อมต่อเซิฟเวอร์ได้ หรือ ไม่มีการเชื่อมต่อ");
179.            }
180.        }
181. 
182.         
183.      
184.      
185.     
186.        private void timer1_Tick(object sender, EventArgs e)
187.        {
188.             
189.             
190. 
191.        }
192. 
193.         
194.    }
195.}




Tag : .NET, C#

Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2016-06-14 19:35:12 By : meatspin View : 1091 Reply : 10
 

 

No. 1



โพสกระทู้ ( 242 )
บทความ ( 0 )



สถานะออฟไลน์


หน้า design ลาก timer มา 1 ตัว และ double click ที่ timer จะได้ even timer1_tick จะทำอะไรก็ใส่ใน even นี้ได้เลยครับ และก็ให้มัน timer1.start ใน form_load ด้วยนะครับ เพื่อไปเรียก timer ให้ ใช้งาน
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-06-15 14:17:11 By : bigsuntat
 

 

No. 2



โพสกระทู้ ( 171 )
บทความ ( 0 )



สถานะออฟไลน์


ลองแล้วครับ ReceiveData(); ไว้ใน Timer1 แต่ Error object reference not set to an instance of an object


ประวัติการแก้ไข
2016-06-15 15:46:23
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-06-15 15:45:56 By : meatspin
 

 

No. 3



โพสกระทู้ ( 242 )
บทความ ( 0 )



สถานะออฟไลน์


ลองไปเช็คที่กระทู้ นี้ครับ

https://www.thaicreate.com/dotnet/forum/123289.html


ผมเคยตอบคำถามไว้แล้ว
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-06-16 02:38:25 By : bigsuntat
 

 

No. 4



โพสกระทู้ ( 74,059 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-06-16 09:19:30 By : mr.win
 

 

No. 5



โพสกระทู้ ( 171 )
บทความ ( 0 )



สถานะออฟไลน์


เชื่อมต่อได้ครับแต่เป็นการกดปุ่มรับค่า แต่พอจะให้ทำงานใน Timer ก็ Error
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-06-16 11:56:56 By : meatspin
 

 

No. 6



โพสกระทู้ ( 242 )
บทความ ( 0 )



สถานะออฟไลน์


เอางี้ส่งโปรแกรม มาให้ผมทางอีเมลล์ที่แจ้งใน PM จะดูให้ครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-06-18 04:08:18 By : bigsuntat
 

 

No. 7



โพสกระทู้ ( 171 )
บทความ ( 0 )



สถานะออฟไลน์


ส่งไปแล้วครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-06-18 09:45:15 By : meatspin
 

 

No. 8



โพสกระทู้ ( 242 )
บทความ ( 0 )



สถานะออฟไลน์


Code (C#)
01.private void BtnCon_Click(object sender, EventArgs e)
02.      {
03.          try
04.          {
05.              client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
06.              IPEndPoint iep = new IPEndPoint(IPAddress.Parse(TxtIP.Text), int.Parse(TxtPort.Text));
07.              client.BeginConnect(iep, new AsyncCallback(connected), client);
08.              timer1.Start();
09.          }
10.          catch
11.          {
12.              MessageBox.Show("เชื่อมต่อเซิฟเวอร์ไม่สำเร็จ โปรดตรวจความถูกต้อง");
13.          }
14.      }



และ


Code (C#)
01.private void BtnDis_Click(object sender, EventArgs e)
02.        {
03.            if (client.Connected)
04.            // receiver.Abort();
05.                receiver.Abort(client);
06.                lblsta.Text = "Disconnected";
07.                client.Close(5);
08.                timer1.Stop();
09.            }
10.            else
11.            {
12.                MessageBox.Show("ไม่มีการเชื่อมต่อ");
13.            }
14.        }



ลองดูครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-06-19 06:54:54 By : bigsuntat
 

 

No. 9



โพสกระทู้ ( 171 )
บทความ ( 0 )



สถานะออฟไลน์


ได้แล้วครับผมขอบคุณมากครับ จมอับเรื่องนี้มาเป็นอาทิตย์
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-06-19 14:58:01 By : meatspin
 

 

No. 10



โพสกระทู้ ( 242 )
บทความ ( 0 )



สถานะออฟไลน์


ยินดีครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-06-19 15:42:52 By : bigsuntat
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : [C#] การสั่งให้ทำงานใน Timer ผมอยากจะให้มันทำงานใน Timer จะเขียนได้ยังไงครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่