Windows应用程序中C#中的并发程序或并行编程 [英] Concurrent Program or Parallel Programming in C# in windows application

查看:146
本文介绍了Windows应用程序中C#中的并发程序或并行编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi Team,



如何在指定时间的Windows应用程序中使用C#创建并发程序或并行编程



例如:



我想分割我的大功能而不是N个小功能。

[N = 1,2,3,....,1000]



这些函数并排运行并执行结果,

我需要非常快地执行这些结果。使用线程



例如:

正常功能执行时间:10秒或更多

使用线程执行时间:我需要不到1秒或更短时间



如何可能创建线程并解决我的问题,

请帮助



并提前致谢。

解决方案

检查这些链接可能会对您有用。



任务并行(任务并行库) [ ^ ]



如何:编写一个简单的Parallel.ForEach循环 [ ^ ]

嗯,你必须设计你的代码尽可能高效。有任何方法可以做到这一点。哪一个是合适的,将在你的PART上进行大量的研究和测试。想出这个。



我们不知道你对C#中的线程有什么了解,也不知道你的要求是什么,所以不可能告诉你要走哪条路。充其量,你会得到建议,但没有什么可以帮助你从一堆研究和测试工作中拯救你。



请记住,无论如何你选择的方法,在计算问题上投掷成堆的线程并不会让你获得越来越多的性能。 CPU一次只能运行这么多线程,而现在在超线程四核CPU上通常只能运行8个或更少。


你不能在true中执行1000个函数并行:除非你的PC有1000多个内核,因为你需要一个内核来处理每个线程。但是,您可以通过手动使用线程或通过 Parallel.For [ ^ ]



但是,请注意创建和启动线程需要时间,因此设置为单个线程的1000个小作业可能比连续完成的相同1000个小作业需要更多实时执行!


Hi Team,

How to create Concurrent Program or Parallel Programming in C# in windows application with specified time

For Example:

I want to split my big functionalities more than N small functions.
[N = 1, 2, 3,...., 1000]

These functions side by side running and execute the result,
I need to execute very fast these result. by using Threading

For Example:
Normal Function execution Time : 10Sec or more
Using Threading Execution Time : I need Only less than 1 sec or less

How to possible to create threading and solve my Issues,
Please help

and Thanks in Advance.

解决方案

Check these links might come handy for you.

Task Parallelism (Task Parallel Library)[^]

How to: Write a Simple Parallel.ForEach Loop[^]


Well, you're going to have to design your code to be as efficient as possible. There are any unmber of ways this can be done. Which one is appropriate is going to take a bunch of research and testing on YOUR PART. to figure this out.

We have no idea what you know about threading in C# nor anything about your requirements so it's impossible to tell you which way to go. At best,m you'll get suggestions, but nothing is going to save you from a bunch of research and testing work.

Keep in mind, that no matter what method you chose, throwing piles and piles of threads at a computational problem does NOT get you more and more performance. A CPU can only run so many threads at one time, and it's usually limited to 8 or less right now on a Hyperthreaded Quad-Core CPU.


You can't execute 1000 functions in true parallel: not unless your PC has 1000+ cores, as you need a core to process each thread. However, you can get up to the number of cores you have running truly in parallel as separate threads, either by using Threading manually, or via Parallel.For[^]

However, be aware that creating and starting threads takes time, so 1000 small jobs set up as individual threads may well take more real time to execute than the same 1000 small jobs done consecutively!


这篇关于Windows应用程序中C#中的并发程序或并行编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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