单击按钮时的方法的线程化
我在单击按钮时调用了一些方法。
functionA()
functionB()
functionC()
这三个函数都是相互独立的,并且执行时间较长。我检查并发现通过线程我可以同时运行这三个,这将节省执行时间。
由于我是线程概念的新手,任何人都可以指导我在场景中执行线程的最简单方法或在这种情况下有用的其他方式。
编辑
同一函数中还有一个问题:
我在三个函数执行后绑定了 5 个 gridview。像这样
gv1.DataSource = GetData("Mill");
gv1.DataBind();
gv2.DataSource = GetData("Factory");
gv2.DataBind();
gv3.DataSource = GetData("Garage");
gv3.DataBind();
gv4.DataSource = GetData("Master");
gv4.DataBind();
他们都使用相同的方法来获取结果,并且他们也需要时间来加载。有什么办法可以让它们并行运行吗?我担心,因为他们使用相同的方法来获取数据。是否可以为它们做线程。如何 ?
I am calling few methods on a button click.
functionA()
functionB()
functionC()
All three functions are independent from each other and they take long time to execute. I checked and found that by threading I can run all three together which will save the execution time.
As I am new to threading concept, could anyone please guide me the simplest way I can do threading in scenario or other way which will be useful in this scenario.
EDIT
One more problem in the same function:
I am binding 5 gridviews after the three functions execution. Like this
gv1.DataSource = GetData("Mill");
gv1.DataBind();
gv2.DataSource = GetData("Factory");
gv2.DataBind();
gv3.DataSource = GetData("Garage");
gv3.DataBind();
gv4.DataSource = GetData("Master");
gv4.DataBind();
They all are using the same method for getting the result and they are also taking time to load. Is there any way I can run them parallel too? I afraid, because they are using same method to get the data. Is it possible to do threading for them. How ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不确定 Parallel.Invoke() 如何决定并行执行什么,但如果您想确保它们并行执行,请使用线程:
或者更好的是,使用 ThreadPool:
记住处理线程异常。
I am not sure how Parallel.Invoke() decides what to execute in parallel, but if you want an assurance that they will execute in parallel, use threads:
Or even better, use the ThreadPool:
Remember to handle your thread exceptions.
最简单的答案是使用 MSDN:Parallel.Invoke()。
您还应该考虑:异步页面。
The simplest answer is to use MSDN: Parallel.Invoke().
You should also consider: Asynchronous Pages.
尝试使用 System.Threading.Tasks 命名空间
类似
http://www.codethinked.com/net -40-and-systemthreadingtasks
Try using System.Threading.Tasks namespace
Something like
http://www.codethinked.com/net-40-and-systemthreadingtasks
下面是一个并行执行 4 个任务的示例:
Here's an example which will execute the 4 tasks in parallel: