VB.Net线程 [英] VB.Net Threading

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

问题描述

此刻我正在研究一个过程,该过程遍历文件夹中的文件;它用作数据迁移的一部分.文件的每次迭代大约需要8秒钟;我需要运行大约30000次,所以8s有点问题.该函数中的操作不能是多线程的,因为它将文档分开,并且需要串行操作.

I am working on a process at the moment which iterates through files in a folder; its used as part of a data migration. Each iteration of a file takes about 8 seconds; I need to run it about 30000 times so 8s that is a bit of a problem. The actions in the function cant be multithreaded as it pulls apart the document and that needs to be serial.

单线程代码执行此操作

    For Each strFile In System.IO.Directory.GetFiles(txtFolderPathIN.Text)
        CallFunction(strFile)
    Next

将其转换为多线程的最佳方法是什么?没有反馈给用户;只需启动该过程并尽快遍历它们.制作此多线程最简单的方法是什么?

What is the best approach to convert this to make it multithreaded? There is no feedback to the user; just need to start the process and iterate through them as quickly as possible. What the easiest way to making this multithreaded?

推荐答案

我会使用线程池.像这样:

ThreadPool.SetMaxThreads = 4

For Each strFile In System.IO.Directory.GetFiles(txtFolderPathIN.Text)
    ThreadPool.QueueUserWorkItem(new WaitCallback(addressof CallFunction), strFile)
Next

您还可以使用自己的线程池系统,方法是拥有线程列表,并使用互斥锁在所有子线程完成运行之前阻止主线程退出.

You could also use your own thread pooling system by having a list of threads and using a mutex to stop the main thread from exiting before all the child threads have finished running.

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

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