当标签被线程更改时,如何修复表单滞后
本文关键字:何修复 表单 滞后 标签 线程 | 更新日期: 2023-09-27 18:21:24
我的应用程序使用(socket-tcp-c#)将文件发送到服务器
文件传输工作正常。。但是显示发送进度的进度表不起作用
它滞后,显示为45mb/s,然后是2mb/s,它不断地上下移动,当我试图移动窗口时,它有点滞后,好像线程出了问题。。我希望你明白发生了什么
如何解决?
Thread thS = new Thread(timeElasped);
thS.Start(); //this code runs when the form show up
下面的代码由进程中的线程执行Form
private void timeElasped()
{
int counter = 0;
while (fileTransfer.busy)
{
rate = (fileTransfer.sum - prevSum);
RateLabel(string.Format("{0}/Sec", CnvrtUnit(rate)));
if(rate!=0)
left = (fileTransfer.fileSize - fileTransfer.sum) / rate;
prevSum = fileTransfer.sum;
TimeSpan t = TimeSpan.FromSeconds(left);
timeLeftLabel(FormatRemainingText(rate, t));
TimeSpan Duration = TimeSpan.FromSeconds(counter);
ElapsedLabel(string.Format("{0:D2}:{1:D2}:{2:D2}", Duration.Hours, Duration.Minutes, Duration.Seconds));
counter++;
Thread.Sleep(1000);
}
}
这是发送文件的代码
public static void sendFile(string filePath)
{
//initialize a thread for progress form
Thread thFP = new Thread(fpRUN);
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
string fileName = Path.GetFileName(filePath);
byte[] fileData;
try
{
//sending file name and file size to the server
busy = true;
fileSize = fs.Length;
byte[] fileDetial = null;
string detail = fileName + "," + fileSize.ToString();
fileDetial = Encoding.ASCII.GetBytes(detail);
client.Send(fileDetial);
//sending file data to the server
fileData = new byte[packetSize];
count = 0;
sum = 0;
// running transfer rate
fileProgress fP = new fileProgress("Sending...");
//show the progress form
thFP.Start(fP);
while (sum < fileSize)
{
fP.ProgressBarFileHandler(sum, fileSize);
fs.Seek(sum, SeekOrigin.Begin);
fs.Read(fileData, 0, fileData.Length);
count = client.Send(fileData, 0, fileData.Length, SocketFlags.None);
sum += count;
}
}
finally
{
busy = false;
fs.Close();
fileData = null;
MessageBox.Show(string.Format("{0} sent successfully", fileName));
}
}
如果您的表单似乎被"阻塞"了一段时间,那么您可能有一个糟糕的设计,因为您的UI线程正在执行阻塞工作。
下面是一个简单但完整的工作线程和一个通知表单的例子,它确保了UI的响应性:
形式:
public partial class Form1 : Form
{
private Worker worker;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
worker = new Worker();
worker.ProgressUpdated += this.worker_ProgressUpdated;
worker.WorkDone += this.worker_WorkDone;
worker.Start();
}
private void worker_WorkDone(object sender, EventArgs e)
{
// Detaches event handlers
// /!' Will be called from a thread different than the UI thread
worker.ProgressUpdated -= this.worker_ProgressUpdated;
worker.WorkDone -= this.worker_WorkDone;
}
private void worker_ProgressUpdated(object sender, ProgressEventArgs e)
{
// Updates the UI
// /!' Will be called from a thread different than the UI thread
this.SetLabelText(string.Format("Percentage: {0}", ((double)e.Value * 100 / (e.Max - e.Min))));
}
private void SetLabelText(string text)
{
// Following required if the method is called from a thread that is not the UI thread
if (this.label1.InvokeRequired)
{
this.label1.Invoke(new MethodInvoker(() => this.SetLabelText(text)));
}
else
{
this.label1.Text = text;
}
}
}
工人等级:
public class ProgressEventArgs : EventArgs
{
public int Value { get; set; }
public int Max { get; set; }
public int Min { get; set; }
}
public class Worker
{
public delegate void ProgressUpdatedEventHandler(object sender, ProgressEventArgs e);
public event ProgressUpdatedEventHandler ProgressUpdated;
public event EventHandler WorkDone;
public void Start()
{
Thread workerThread = new Thread(new ThreadStart(this.DoWork));
workerThread.Start();
}
private void DoWork()
{
int min = 0;
int max = 1000000;
for (int i = min; i < max; i++)
{
// Simulates work
////System.Threading.Thread.Sleep(1);
// Notify of progress update
////this.OnProgressUpdate(min, max, i);
// Notify of progress update but not every time to save CPU time
// Uses mod function to do the job 1 out of 100 times
if (i % 100 == 0)
{
this.OnProgressUpdate(min, max, i);
}
}
// Notify the work is done
if (this.WorkDone != null)
{
this.WorkDone(this, EventArgs.Empty);
}
}
private void OnProgressUpdate(int min, int max, int value)
{
if (this.ProgressUpdated != null)
{
this.ProgressUpdated(this, new ProgressEventArgs { Max = max, Min = min, Value = value });
}
}
}