gpt4 book ai didi

c# - l versC#多线程问题

转载 作者:行者123 更新时间:2023-11-30 23:07:43 24 4
gpt4 key购买 nike

当文件复制线程正在运行时,我无法让 ui 线程更新 ui。我的最终目标是让动画继续旋转,直到大文件复制最终完成,让用户知道程序没有卡住。这是一个非常简单的服务器到服务器文件复制程序。

谁能告诉我我做错了什么?

enter image description here Simple Windows Form

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.Threading;

using System.IO;
using System.Threading.Tasks;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void ResetProgress()
{
lblStep1.Image = null;
}

private void SetupProgress()
{
lblStep1.Image = global::animation1.Properties.Resources.animation;
}

private void fileCopy()
{
File.Copy("large file source", "large file destination", true);
}

private void Form1_Load(object sender, EventArgs e)
{
lblStep1.Image = global::animation1.Properties.Resources.animation;
}

private async void button1_Click(object sender, EventArgs e)
{
SetupProgress();
await Task.Run(() => fileCopy());
ResetProgress();
}

private void btnStop_Click(object sender, EventArgs e)
{
// unhandled currently
}
}
}

* 原始版本 *

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.Threading;

using System.IO;
using System.Threading.Tasks;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Thread workItemsProducerThread;
private Thread workItemsCopyThread;
public Form1()
{
InitializeComponent();
}

private void ResetProgress()
{
lblStep1.Image = null;
}

private void SetupProgress()
{
this.BeginInvoke((MethodInvoker)delegate ()
{
lblStep1.Image = global::animation1.Properties.Resources.animation;
});
}

private void fileCopy()
{
File.Copy("Large file source", "Large file destination", true);

this.BeginInvoke((MethodInvoker)delegate ()
{
MessageBox.Show("Done");
});

}

private void Form1_Load(object sender, EventArgs e)
{
lblStep1.Image = global::animation1.Properties.Resources.animation;
}

private void btnStart_Click(object sender, EventArgs e)
{
this.workItemsProducerThread = new Thread(new ThreadStart(this.SetupProgress));
this.workItemsProducerThread.IsBackground = true;
this.workItemsProducerThread.Start();

this.SetupProgress();

this.workItemsCopyThread = new Thread(new ThreadStart(this.fileCopy));
this.workItemsCopyThread.IsBackground = true;
this.workItemsCopyThread.Start();


while (workItemsCopyThread.IsAlive)
{
Thread.Sleep(1000); // wait
}

MessageBox.Show("Done");
}

private void btnStop_Click(object sender, EventArgs e)
{
if (this.workItemsProducerThread != null)
{
this.workItemsProducerThread.Abort();
lblStep1.Image = global::animation1.Properties.Resources.animation;
}
}

private void btnTest_Click(object sender, EventArgs e)
{
fileCopy();
}
}

最佳答案

不要睡在你的点击处理程序中。这会卡住 UI 线程。让时钟处理程序退出。在您的文件复制线程中,当复制不是时。使用 Invoke(或 BeginInvoke)使已完成的消息框在 UI 线程上弹出。

关于c# - l versC#多线程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47053774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com