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;
using System.Net.Sockets;
using System.IO;
using System.IO.Ports;
using System.Threading;
using System.Runtime.InteropServices;
namespace GetCode
{
public partial class Frm_Main : Form
{ byte[] recvBytes = new byte[1024];
byte[] dataBuff = new byte[7];
static string shun_liusu;
static string shun_liuliang;
static string leiji;
// Socket sockC;
TimeOutSock timeSock = new TimeOutSock();
Thread thrCli;
private delegate void FlushClient();//代理
//Thread thread = null; public Frm_Main()
{
InitializeComponent();
}
private void View1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{ } private void Frm_Main_Load(object sender, EventArgs e)
{ while (true)
{
byte[] retdata = new byte[] { 0x55, 0xAA, 0x04, 0x00, 0x00, 0x00, 0x00, 0x48, 0x10, 0x5C, 0x79, 0x00, 0x00, 0x00, 0x31 };
//byte[] retdata1 = new byte[] { 0x55, 0xAA, 0x03, 0x00, 0x00, 0x00, 0x00, 0x48, 0x10, 0x32, 0x5B, 0x00, 0x00, 0x00, 0xE8 };
byte[] shunshibuff = new byte[4];
byte[] liusubuff = new byte[4];
byte[] leijiliangbuff = new byte[4]; shunshibuff[3] = retdata[3];
shunshibuff[2] = retdata[4];
shunshibuff[1] = retdata[5];
shunshibuff[0] = retdata[6]; shun_liuliang = BitConverter.ToSingle(shunshibuff, 0).ToString("0.00");
liusubuff[3] = retdata[7];
liusubuff[2] = retdata[8];
liusubuff[1] = retdata[9];
liusubuff[0] = retdata[10];
shun_liusu = BitConverter.ToSingle(liusubuff, 0).ToString("0.00"); leijiliangbuff[3] = retdata[11];
leijiliangbuff[2] = retdata[12];
leijiliangbuff[1] = retdata[13];
leijiliangbuff[0] = retdata[14];
leiji = BitConverter.ToUInt32(leijiliangbuff, 0).ToString();
if (retdata[2] == 01)
{
label1.Text = shun_liuliang + "\r\n";
label1.Refresh();
label1.Text += leiji + "\r\n";
label1.Refresh();
label1.Text += shun_liusu + "\r\n";
label1.Refresh();
}
else if (retdata[2] == 02)
{
label2.Text = shun_liuliang + "\r\n";
Application.DoEvents();
label2.Text += leiji + "\r\n";
Application.DoEvents();
label2.Text += shun_liusu + "\r\n";
Application.DoEvents();
}
else if (retdata[2] == 03)
{
label3.Text = shun_liuliang + "\r\n";
Application.DoEvents();
label3.Text += leiji + "\r\n";
Application.DoEvents();
label3.Text += shun_liusu + "\r\n";
Application.DoEvents();
}
else if (retdata[2] == 04)
{
this.label4.Text = shun_liuliang + "\r\n";
label1.Refresh();
label4.Text += leiji + "\r\n";
label1.Refresh();
label4.Text += shun_liusu + "\r\n";
Application.DoEvents();
}
}
}
public void printData(string str1)
{
//创建文件流对象实例
string path = "d:/logs/";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path = path + DateTime.Today.ToString("yyyyMMdd") + "RecDataLog.txt";
FileStream fs = new FileStream(path, FileMode.Append);
StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312"));
try
{
sw.WriteLine(str1);
}
catch (Exception ex)
{
printData(ex.Message);
}
finally
{
sw.Close();
fs.Close();
}
}
private void label1_Click(object sender, EventArgs e)
{}
我希望能让4个label根据输入的不同不断地刷新,但是这个程序不出现窗口,一直在循环,网上说需要线程控制,但是参数调用时,我不知道该怎么改相应的地方,希望大家能多帮帮我,我刚开始学习C#,不是太懂。C#线程循环label控件Path
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;
using System.Net.Sockets;
using System.IO;
using System.IO.Ports;
using System.Threading;
using System.Runtime.InteropServices;
namespace GetCode
{
public partial class Frm_Main : Form
{ byte[] recvBytes = new byte[1024];
byte[] dataBuff = new byte[7];
static string shun_liusu;
static string shun_liuliang;
static string leiji;
// Socket sockC;
TimeOutSock timeSock = new TimeOutSock();
Thread thrCli;
private delegate void FlushClient();//代理
//Thread thread = null; public Frm_Main()
{
InitializeComponent();
}
private void View1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{ } private void Frm_Main_Load(object sender, EventArgs e)
{ while (true)
{
byte[] retdata = new byte[] { 0x55, 0xAA, 0x04, 0x00, 0x00, 0x00, 0x00, 0x48, 0x10, 0x5C, 0x79, 0x00, 0x00, 0x00, 0x31 };
//byte[] retdata1 = new byte[] { 0x55, 0xAA, 0x03, 0x00, 0x00, 0x00, 0x00, 0x48, 0x10, 0x32, 0x5B, 0x00, 0x00, 0x00, 0xE8 };
byte[] shunshibuff = new byte[4];
byte[] liusubuff = new byte[4];
byte[] leijiliangbuff = new byte[4]; shunshibuff[3] = retdata[3];
shunshibuff[2] = retdata[4];
shunshibuff[1] = retdata[5];
shunshibuff[0] = retdata[6]; shun_liuliang = BitConverter.ToSingle(shunshibuff, 0).ToString("0.00");
liusubuff[3] = retdata[7];
liusubuff[2] = retdata[8];
liusubuff[1] = retdata[9];
liusubuff[0] = retdata[10];
shun_liusu = BitConverter.ToSingle(liusubuff, 0).ToString("0.00"); leijiliangbuff[3] = retdata[11];
leijiliangbuff[2] = retdata[12];
leijiliangbuff[1] = retdata[13];
leijiliangbuff[0] = retdata[14];
leiji = BitConverter.ToUInt32(leijiliangbuff, 0).ToString();
if (retdata[2] == 01)
{
label1.Text = shun_liuliang + "\r\n";
label1.Refresh();
label1.Text += leiji + "\r\n";
label1.Refresh();
label1.Text += shun_liusu + "\r\n";
label1.Refresh();
}
else if (retdata[2] == 02)
{
label2.Text = shun_liuliang + "\r\n";
Application.DoEvents();
label2.Text += leiji + "\r\n";
Application.DoEvents();
label2.Text += shun_liusu + "\r\n";
Application.DoEvents();
}
else if (retdata[2] == 03)
{
label3.Text = shun_liuliang + "\r\n";
Application.DoEvents();
label3.Text += leiji + "\r\n";
Application.DoEvents();
label3.Text += shun_liusu + "\r\n";
Application.DoEvents();
}
else if (retdata[2] == 04)
{
this.label4.Text = shun_liuliang + "\r\n";
label1.Refresh();
label4.Text += leiji + "\r\n";
label1.Refresh();
label4.Text += shun_liusu + "\r\n";
Application.DoEvents();
}
}
}
public void printData(string str1)
{
//创建文件流对象实例
string path = "d:/logs/";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path = path + DateTime.Today.ToString("yyyyMMdd") + "RecDataLog.txt";
FileStream fs = new FileStream(path, FileMode.Append);
StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312"));
try
{
sw.WriteLine(str1);
}
catch (Exception ex)
{
printData(ex.Message);
}
finally
{
sw.Close();
fs.Close();
}
}
private void label1_Click(object sender, EventArgs e)
{}
我希望能让4个label根据输入的不同不断地刷新,但是这个程序不出现窗口,一直在循环,网上说需要线程控制,但是参数调用时,我不知道该怎么改相应的地方,希望大家能多帮帮我,我刚开始学习C#,不是太懂。C#线程循环label控件Path
解决方案 »
- 用vs连接数据库问题
- GetStockObject(DEFAULT_GUI_FONT)中的DEFAULT_GUI_FONT是如何定义的,怎么用的
- display image in a full screen window?
- Datagrid中的CommandArgument取不到值啊!急用,紧急求救!
- vs2005,WinForm中如何获取DataGrid中某行某列的控件,并设置控件属性?
- window.showModalDialog传值问题!
- winform的DataGrid問題
- 菜鳥提間:求一從服務器下載文檔的方法,謝謝。
- hashtable如何处理这样的问题?
- 请教C#高手
- 如何获取treeview选中的层数?
- 下载文件报错 求大神给解释!!!
this.Show();
//主线程
public delegate void UpdateMessage(string mes);
public void UpdatePortMessage(string mes)
{
this.txtMessage.AppendText(string.Format("Connected {0}. succeed", mes) + "\r\n");
this.txtClientIP.Text = mes;
}
// 线程1
UpdateMessage upPort = new UpdateMessage(UpdatePortMessage);
this.BeginInvoke(upPort, point);