【发布时间】:2023-03-21 20:43:01
【问题描述】:
在我的应用程序中,我有两个表单(这是我的第一个相当大的应用程序)
点击父窗体中的开始按钮后,我希望加载面板出现,并完成一些逻辑。
加载面板(它只是另一个寡妇形式)包含 bunifu 加载圆圈动画(和一些文本)。 逻辑部分负责从目录树中收集名称,然后替换树上 Ms.Word 文件中的一些文本。
当我在不执行逻辑的情况下打开加载面板时,加载面板动画正常,一切正常。
private void bunifuFlatButton1_Click(object sender, EventArgs e)
{
int x = this.Location.X+this.Width/2-75;
int y = this.Location.Y +this.Height/2-175;
Loader_panel LP = new Loader_panel();
LP.Left = x;
LP.Top = y;
LP.Show();
//System.Threading.Thread.Sleep(5000); \\this doesn't help animation to start
if (FormLogic._dataList.Count > 0) \\Here Logic part starts
{
for (int i = 0; i < FormLogic._dataList.Count; i++)
GetDir.GetTarget(FormLogic._dataList[i]);
/*foreach (var directory in FormLogic._dataList)
GetDir.GetTarget(directory);*/
LogList.Items.Add(DateTime.Now + "List isn't empty");// for testing
FormLogic.ClearData();
}
LP.Close();
}
启用逻辑加载面板后出现(外观不流畅),但动画不起作用(仅在逻辑部分完成工作时才开始工作 - 我通过禁用 LP.Close() 对其进行了测试。可以是什么这个问题的原因?
附加问题。在 .NET 环境中,代码被编译为与多个处理器线程一起工作,还是我必须手动完成?
编辑 06/08/2018 7:21 CEST
我无法从 GetDir 方法访问 LogList(由于其他线程处理它)。 我尝试了多个 Invoke 构造,但似乎都不起作用;/ 我只是太菜鸟,无法弄清楚。我在下面的代码中指定了更多细节:
namespace Docr
{
public partial class DocrForm : Form
{
.....
private async void Button1_Click(object sender, EventArgs e)
{
int x = this.Location.X + this.Width / 2 - 75;
int y = this.Location.Y + this.Height / 2 - 175;
Loader_panel LP = new Loader_panel();
LP.Left = x;
LP.Top = y;
LP.Show(); //animation
int count = FormLogic._dataList.Count;
var list = FormLogic._dataList;
await Task.Run(() =>// processing logic during showing animation
{
if (count > 0)
{
for (int i = 0; i < count; i++)
{
GetDir.GetTarget(list[i],LogList); // Passing LogList as and argument
}
Invoke((Action)(() => { LogList.Items.Add(DateTime.Now + "Hi LogList"); }));\\ works fine
}
});
FormLogic.ClearData();
LP.Close();
}
....
}
namespace DocrLogic
{
class GetDir
{
.....
static public void GetTarget(string UserDirectory, ListBox List)// passing ListBox as an Argument
{
var path = UserDirectory;
var TargetDir = new DirectoryInfo(path);
var AllDocs1 = TargetDir.GetFiles("*.doc*", SearchOption.AllDirectories);
var ProperPrefixes = new List<string> { };
Invoke((Action)(() => { List.Items.Add(DateTime.Now + "Hi Log List, GetDir here"); })); // THIS DOESN'T WORK
....
}
.....
}
}
【问题讨论】:
-
逻辑需要使用线程或任务。
标签: c# winforms performance