如何实现“好像”多个exes从C#中的一个exe运行 [英] How to achieve “as if” multiple exes running from one exe in C#

查看:66
本文介绍了如何实现“好像”多个exes从C#中的一个exe运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你可以告诉我是否可以实现以下任务,如果是,那么如何,如果没有那么原因。


我有用C#编写的邮件功能,我需要一次发送50封邮件。现在我可以通过两种方式实现这一点:一次运行50个exes或者通过在单个程序中实现线程概念。



但是正如预期的那样,线程概念并没有给我带来结果或者可以通过运行多个exes来表达我的表现。你可以通过提供运行一个程序的建议或概念来帮助我,并获得运行多个exes的性能。

解决方案

使用并行编程 [ ^ ]。这意味着如果您有多个核心,则可以使用计算机中的不同核心。您可以在不同的核心上分配您的工作,从而提供更好的性能。


首先,50封电子邮件几乎没有什么可以关心性能的。线程不会大大提高吞吐量,因为瓶颈就是网络。此外,当您创建的线程数远远超过核心数时,由于线程开销,您可能会使吞吐量变差。你需要了解线程的主要目的不是性能。



你缺少最明显和实用的解决方案:发送50条消息,你不需要50条流程。您只能运行一个按顺序发送50条消息的流程。



新年快乐!



-SA


正如已经提到的,这不是您的应用程序的问题或源代码的问题。真正的问题是您要通过其发送电子邮件的网络连接。电子邮件是一个循环,执行为,



连接到SMTP服务器
| - >转让凭证
| - >发送数据,收件人,主题和其他字段
| - >得到回复
| - >转回回复
| - >如果要发送更多电子邮件,请重复此





..因此,主要因素是您正在使用的连接。谢尔盖是对的,线程也无济于事它会在你自己的系统上创建一个新问题,在机器上执行更多的操作。线程可能适用于你要进入异步编程的地方,但只发送50封电子邮件,启动50个进程是疯狂的想法。是的,最好创建一个异步方法,每次以50个循环等方式异步发送这些电子邮件。有关异步编程的更多信息,请阅读此MSDN文档 [ ^ ]以及其中附带的所有其他资源。



一个简单的循环,用于通过我的8MB互联网发送5封电子邮件使用Gmail服务器连接需要10秒钟。如果我必须改进这一点,我会尝试增加带宽以允许发送和接收更多数据,我也会尝试从服务器获得任何高级订阅(如果他们允许的话)以提高他们的响应率


Can you let me know whether following tasks is achievable or not, if yes then how, if no then why.

I have got mailing function written in C#, i need to send 50 mails at one shot. Now i can achieve this by two ways either run 50 exes at one time or by implementing threading concept in single program.

But as expected threading concept is not giving me result or can say performance as i am getting by running multiple exes. Can you help me by giving suggestion or concept for running one program and get the performance of running multiple exes.

解决方案

Use parallel programming[^]. This means that you make use of the different cores in your computer, if you have multiple. You can distribute your work across the different cores, which gives better performance.


First of all, 50 e-mails is next to nothing to care about performance that much. Threading does not improve your throughput much, as the bottleneck would be the network. Moreover, when you create number of threads which is much more than the number of your cores, you can make your throughput worse, due to overhead of threads. You need to understand the primary purpose of threads is not performance.

You are missing the most apparent and practical solution: to send 50 messages, you don't need 50 processes. You can run only one process which sends 50 messages sequentially.

Happy New Year!

—SA


As already have been mentioned that this is not your application's problem or the source code's problem. The problem in real is the network connection through which you're going to send the emails. And email is a cycle, which is executed as,

Connect to SMTP Server
|-> Transfer Credentials
    |-> Send data, recipient, subject and other fields
    |-> Get response
    |-> Transfer response back
    |-> If more emails to be sent, repeat this



.. so the major factor to this is the connection that you're using. Sergey is right, threading won't help infact it will create a new problem on your own system, more executions on the machine. Threading might be applied where you are to go into an async programming or so, but just to send 50 emails, starting 50 processes is insane idea. Yes, it will be a good idea to create an async method that would send these emails asynchronously each time and in 50 loops etc. For more on Async programming, please read this MSDN documentation[^] and all other resources attached in it.

A simple loop, meant to send the 5 emails through my 8MB internet connection using Gmail server would take more like 10 seconds. If I had to improve this, I would try to increase the bandwidth to allow more data to be sent and recieved and I would also try to get any premium subscription from the server (if they allow any) to improve the response rate from their side.


这篇关于如何实现“好像”多个exes从C#中的一个exe运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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