Note that there are some explanatory texts on larger screens.

plurals
  1. POThread Pool Optimization
    primarykey
    data
    text
    <p>I am working on a Multi-Threaded Async form application which sends mass emails. I nearly finished the application but have concerns about the performance. If you enlighten me with better best practices, it will be perfect and I will really learn basics as I am a junior developer.</p> <p>Here is the question : First I have Start button for the application which triggers a Windows.Forms.Timer component.</p> <pre><code>private void btnStart_MouseClick(object sender, MouseEventArgs e) { timerNewCampaignChecker.Tick += new EventHandler(timerNewCampaignChecker_Tick); timerNewCampaignChecker.Enabled = true; timerNewCampaignChecker.Start(); } </code></pre> <p>Application needs to check database every 3 seconds for new campaigns(1st method), customize the the campaign details(2nd one) inline to user input and send the campaign to receivers via email(3rd one). In order to check database every 3 seconds for new campaigns I have the above timer(interval is 3 seconds) that starts the flow by checking new campaigns: </p> <pre><code>MethodInvoker invoker; private void timerNewCampaignChecker_Tick(object sender, EventArgs e) { invoker = new MethodInvoker(CheckNewCampaigns); invoker.BeginInvoke(null, null); } </code></pre> <p>So, the timer triggers CheckNewCampaigns method in every 3 seconds and let the process begin from its 1st step. <strong>Is it right to start the flow and ThreadPool structure from the timer itself???</strong> Now CheckNewCampaigns checks the DB, creates a List object, and for every campaign, it sends the details of the object to another method by invoking ThreadPool.</p> <pre><code>delegate bool StepCaller(int campaignID); private void CheckNewCampaigns() { StringBuilder builder = new StringBuilder(); StepCaller stepCaller = new StepCaller(PrepareCampaignEmail); IEnumerable&lt;Campaigns&gt; CampaignsList = DatabaseManager.GetCampaignsList(DatabaseManager.GetNewCampaigns()); foreach (Campaigns Campaign in CampaignsList) { stepCaller.BeginInvoke(Campaign.CampaignID, new AsyncCallback(PrepareEmailCallback), null); } } </code></pre> <p><strong>Do I have to code endInvoke in the first method, or should I just fire and forget??</strong>. Because the most important thing is that application has a flow, parameters are passed between methods, 2nd method should know when 1st one is finished so that it can take the campaignID and customize it. But while doing these, a new flow should start and check new campaigns which is 1st method. And also emails should be sent at the same moment. The New method(2nd one) <strong>customizes the campaign</strong> and invokes another method(3rd one) by using ThreadPool also. Here it is :</p> <pre><code>private bool PrepareCampaignEmail(int campaignID) { EmailCaller emailCaller = new EmailCaller(SendEmail); IEnumerable&lt;Subscribers&gt; SubscribersList = DatabaseManager.GetCampaignSubscribersList(DatabaseManager.GetCampaignSubscribers(campaignID)); CampaignCustomazition campaignCustomizer; List&lt;CampaignCustomazition&gt; campaignCustomizedList = new List&lt;CampaignCustomazition&gt;(); // foreach in içindeki beginInvoke ı dışarıya çıkardığımda kullanılacak foreach (Subscribers subscriber in SubscribersList) { campaignCustomizer = new CampaignCustomazition(campaignID); campaignCustomizer.CustomizeSource(campaignID, out campaignCustomizer.source, out campaignCustomizer.format); campaignCustomizer.CustomizeCampaignDetails(campaignID, out campaignCustomizer.subject, out campaignCustomizer.fromName, out campaignCustomizer.fromEmail, out campaignCustomizer.replyEmail); campaignCustomizer.CustomizeSubscriberDetails(campaignID, out campaignCustomizer.subscriberID, out campaignCustomizer.email, out campaignCustomizer.fullName); IAsyncResult result = emailCaller.BeginInvoke(campaignCustomizer, null, null); try { emailCaller.EndInvoke(result); } catch(Exception ex) { Trace.WriteLine(ex.Message); } } return false; } </code></pre> <p>I guess there is a main problem in this second step.There is a database operation for 5-6 times nearly. This can a be aproblem for the thread maybe. It seems working fine, but my way can be wrong. There can be tens of campaign one day. Is it a problem, or is my way fine?(DB operations are not so harsh and complicated by the way).</p> <p>After preparing email, it is ready to be sent which is invoked above. Third method is a simple SMTP method that sends email and logs it.</p> <p>This last method also does not have EndInvoke function but this is the last method of the application as mail just sent.</p> <p><strong>Does my way of dealing with timer and ThreadPool seem logical to you?? How may I test the performance in best way and optimize the working of threadpool?? What can you suggest and teach for any of the above?</strong></p> <p>Thanks alot</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload