How to send via multi-threaded polling for outgoing messages

Source: Internet
Author: User
Tags add time

By default, a C # program has a thread. This thread executes the code in the program that starts and ends with the Main method. Each command that main executes directly or indirectly is executed by the default thread (or the main thread), and the thread terminates when main returns. However, you can create worker threads to execute code in parallel with the main thread. These threads are often referred to as "worker threads."

Multithreading solves the problem of throughput and responsiveness, but it also introduces resource sharing issues such as deadlocks and contention states. Multithreading is especially useful for tasks that require different resources, such as file handles and network connections. Assigning multiple threads to a single resource can cause synchronization problems, and threads are frequently blocked to wait for other threads, which runs counter to the purpose of using multithreading.

Below I share-how to send via multi-threaded polling for outgoing messages. Implementation process: Design the Outbox data table, configure the sending server, develop the WinForm program to send.

1. Designing the Outbox
Outbox records send messages that include-senders, recipients, message headers, message content, add time, send time, and send failure messages.

2. Send mail Configuration
Send mail in C # SMTP protocol, need to set up sender and send server related information

3. Implement the WinForm Program
The program interface is designed for toolbars, messages to be sent, messages to be sent, and information for sending processes to be displayed. The program is designed for two timers, one to poll for outgoing messages, and one to send mail.


3.1. Read the database for outgoing messages
Use the timer to poll your Outbox to read the messages you want to send.


3.2. Thread sending mail
Use the thread to invoke the outgoing mail component for message delivery.


3.3. Mail delivery processing
After the send is successful, the status of the update to send message is "sent", and when the send fails, the status of the message to be sent is "pending" and the next round time is set to 30 minutes.


At this point, through multi-threaded polling to send messages to send the message to achieve the sharing completed. Multithreading is difficult to debug in C # development, which requires a detailed record of the program's sending logs and reports on the execution of the thread for the developer to maintain. The use of threads (thread) to send messages, it is easy to cause the program to feign animation, to do this, I use the way the timer to call the thread (thread).

How to send via multi-threaded polling for outgoing messages

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.