使用不同的参数调用相同的方法 - 如何使用多线程/并行类? [英] Calling same method with different parameters - How to use multithreading / parallel class?

查看:142
本文介绍了使用不同的参数调用相同的方法 - 如何使用多线程/并行类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个场景:我有一个dll,它有从db获取数据的方法,取决于传递的参数,进行各种检查并给我所需的数据。

GetGOS_ForBill(AgencyCode)

在Windows应用程序中,我有列出500 +代理商的列表框。我检索每个代理商的GOS附加到通用列表。如果用户选择了所有代理商(现在为500 +),则大约需要10分钟。从dll返回数据。



我们关于后台处理。但除了用户在屏幕上做其他事情之外,这并没有减少时间。考虑多线程或并行类。



有人可以帮我吗?什么是正确的方法,我们如何实现这一目标?

Here is the scenario: I have a dll which has method that gets data from db, depending on parameters passed, does various checks and gives me required data.
GetGOS_ForBill(AgencyCode)
In a windows application, I have listbox which list 500 + agencies. I retrieve GOS for each agency append to a generic list. If the user has selected all agencies (500 + for now), it takes about 10 min. to return data from the dll.

We though about background processing. But that doesn''t reduce the time, other than user get to do other things on the screen. Considering multithreading or parallel class.

Can anybody help me with this? What would be right approach and how can we accomplish this?

推荐答案

多线程或并行可能会或可能不会加速 - 它实际上可以减慢因为处理设置线程/任务并为它们分配单独的内存空间的开销。此外,它还取决于其他内容 - 如果每个线程需要与SQL交谈,那么每个线程可能会使用两个内核。



我会做的第一件事看看你有什么,并开始计时。 (使用秒表类)尝试找出操作的哪个部分需要时间,可能是您可以进行简单的更改,从而大大减少操作。例如,如果您为进入列表的每个项目访问数据库,那么您可能会发现单次旅行更快,然后将数据排序。至少如果你已经计算出每个部分需要多长时间,你可以定位你的变化,并量化它们是否值得解决。



(我有一个在启动时从数据库加载的列表,使用后台任务和启动画面加载1000多个项目花了两分半钟。通过计算浪费时间的时间,我把它减少到12秒,30,000个项目,并抛弃了飞溅)
Multithreading or parallel may or may not speed things up - it can actually slow things down because of the processing overhead in setting up the thread / tasks and allocating separate memory space for them. In addition it depends what else is going on - if each thread needs to talk to SQL then that will potentially use two cores per thread.

The first thing I would do is look at what you have, and start timing it. (Use the Stopwatch class) Try to find which part of the operation is taking time, it may be that you can make a simple change which cuts it dramatically. For example, if you are doing a trip to your DB for each item going into the list, then you may find it a lot quicker to do a single trip then sort the data out afterwards. At least if you have worked out how long each part takes, you can both target your changes, and quantify them as to whether they are worthwhile solutions or not.

(I had a list which loaded from a database on startup, and it took two and half minutes to load 1000+ items using a background task and a splash screen. By timing what was wasting time, I got it down to 12 seconds for 30,000 items, and dumped the splash)


这篇关于使用不同的参数调用相同的方法 - 如何使用多线程/并行类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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