VB.Net Win窗体的多线程或BackgroundWorker [英] Multi-thread or BackgroundWorker for VB.Net Win Forms

查看:92
本文介绍了VB.Net Win窗体的多线程或BackgroundWorker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是BackgroundWorker类和多线程的新手,但是我认为这是我所需要的.我有一个用VB.Net编写的程序,该程序允许用户将多个CAD零件加载到ListView中并执行多项任务,例如将零件复制到目录中以进行嵌套,创建IGES文件,创建PDF''从图纸中输入,然后将零件数据输入到AS400数据库(BOM)中. BOM表条目花费的时间最长(大约30到45秒),因为它加载了一个表格,该表格读取属于BOM表的所有零件并在TreeView中显示它们,从而使用户能够选择应插入零件的级别.我想做的是能够在用户单击处理"按钮后打开BOM表表单,并在处理所有其他内容的同时在后台运行它,然后在完成所有其他操作后显示BOM表.我已经在Google上搜索并阅读了几篇有关BackgroundWorker类和多线程的文章,但没有找到与整个表单有关的任何内容.任何帮助将不胜感激.提前谢谢您.

问候,
Shawn

I''m new to the BackgroundWorker Class and Multi-threading, but I think something like this is what I''m needing. I''ve got a program I''ve written in VB.Net that allows a user to load in several CAD parts into a ListView and perform several tasks like copy parts to a directory for nesting, create IGES files, create PDF''s from drawings, and input part data into AS400 databases (BOM). The BOM entry takes the longest (about 30 to 45 seconds) because it loads a form that reads all parts belonging to the BOM and shows them in a TreeView to give the user the ability to select at what level the part should be inserted. What I would like to do is be able to open the BOM form after the user clicks the Process button and have it run in the background while all of the other stuff is processed, then have the BOM form show after everything else has been completed. I''ve Googled and read several articles regarding the BackgroundWorker Class and Multi-threading, but haven''t found anything that pertained to an entire form. Any help will be appreciated. Thank you in advance.

Regards,
Shawn

推荐答案

我不能在这个问题上提供很多帮助,因为我只是在学习这一点,但是您可以将加载代码放在一个sub/function声明为async并使用await调用它(代码在函数完成后在某处显示表单)?如果我对async/await有了基本的了解,那应该做你想要的.当然,您会希望自己进行一些研究,以弄清楚该怎么做(当然,看看我是否正确).
I can''t provide a lot of help on the subject because I''m just learning about this, but could you put the loading code in a sub/function declared async and call it using await (with the code to show the form somewhere after the function finishes)? If I''ve got the basic understanding of async/await figured out, that should do what you want. You''ll, of course, want to do some research on your own to figure out exactly how to do it (and to see if I''m right, of course).


后台工作者是在背景中加载数据的理想选择.您可以在网络上找到很多有关它们的信息,这并不难使它们起作用.

通常,您可以每1%的进度报告进度,并在该进度表上更新UI(将已处理的项目添加到列表中,并可能更新进度条).

鉴于现在大约需要50秒,因此您的UI每秒大约更新两次,每1%更新一次(也可以每x个项目更新一次).

您可以尝试一下.在您的情况下,由于处理时间非常长,您可能希望每1%的增量进行更新.

您希望经常报告进度以确保流畅性,但又不要过于频繁地影响整体性能.
A background worker is a perfect choice for loading data in backgground. You can find a lot of information on them on the web and it not hard at all to make them work.

Typically, you can report progress every 1% of progress and update your UI at that (add processed item to the list and possibly update a progress bar).

Given that it take around 50 seconds now, your UI would be updated about twice per second with an update every 1% (it can also be every x items).

You can experiment with that. In your case, you will probably want more than an update per 1 percent increment since it is a very long processing.

You want to report progress often enough for smoothness but not too often to aftect the overall performance.


这篇关于VB.Net Win窗体的多线程或BackgroundWorker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆