PowerTCP Mail for .NET
Start Method
Example 




System.Threading.WaitCallback delegate for your function "void worker(object)".
Information to pass to the delegate method; can be null.
Start a worker thread and execute the specified delegate.
Syntax
Public Sub Start( _
   ByVal worker As WaitCallback, _
   ByVal state As Object _
) 
Dim instance As MailBase
Dim worker As WaitCallback
Dim state As Object
 
instance.Start(worker, state)
public void Start( 
   WaitCallback worker,
   object state
)
public: void Start( 
   WaitCallback* worker,
   Object* state
) 
public:
void Start( 
   WaitCallback^ worker,
   Object^ state
) 

Parameters

worker
System.Threading.WaitCallback delegate for your function "void worker(object)".
state
Information to pass to the delegate method; can be null.
Remarks

This method provides a convenient way to execute any method on a worker thread. Applications with a UI thread can use this technique to execute methods on a worker thread that would "freeze" the UI if executed on the UI thread.

Uncaught exceptions occurring on the worker thread will be automatically reported by raising the Error event.

Internally, this method uses ThreadPool.QueueUserWorkItem to start a worker thread. This method is provided for convenience; the developer may use alternative methods for starting worker threads if more specialized use is required.

Example
Demonstrates using Start(), Marshal() and the UserState event. Demonstrates sending a MailMessage on a worker thread.
private void button1_Click(object sender, EventArgs e)
{
    smtp1.Start(myBlockingFunction, myObject);
}

private void myBlockingFunction(object myObject)
{
    //This executes on a worker thread, so it could contain 
    //any long-running code, and still not block the UI
    smtp1.Marshal("Message from the worker thread", null);
}

void smtp1_UserState(object sender, UserStateEventArgs e)
{
    MessageBox.Show(e.Message);
}
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
    smtp1.Start(AddressOf myBlockingFunction, myObject)
End Sub

Private Sub myBlockingFunction(ByVal myObject As Object)
    'This executes on a worker thread, so it could contain 
    'any long-running code, and still not block the UI
    smtp1.Marshal("Message from the worker thread", Nothing)
End Sub

Private Sub smtp1_UserState(ByVal sender As Object, ByVal e As UserStateEventArgs)
    MessageBox.Show(e.Message)
End Sub
private void button1_Click(object sender, EventArgs e)
{
    //Send a message to the specified recipient
    //The Start method is used to execute the operation on a worker thread
    //(Event handlers were added in the designer)
    smtp1.Start(sendMail, "myRecipient@dart.com");
}

private void sendMail(object state)
{
    //This function executes on a worker thread without blocking the UI
    //Create message to send
    MailMessage message = new MailMessage();
    message.To = state as string;
    message.From = "sender@dart.com";
    message.Subject = "Here is your file";
    message.Text = "Please see the attached file.";
    message.Attachments.Add(new Attachment("myAttachment.doc"));

    //Set session parameters
    smtp1.Session.RemoteEndPoint =
        new Dart.Mail.IPEndPoint("myMailServer", Smtp.GetDefaultPort(smtp1.Session));
    smtp1.Session.Username = "myUsername";
    smtp1.Session.Password = "myPassword";

    //Send message and logout
    smtp1.Send(message);
    smtp1.Close();
}

private void smtp1_Connection_Log(object sender, DataEventArgs e)
{
    //Log the communication to a textbox
    textBox1.AppendText(e.Data.ToString());
}

private void smtp1_Error(object sender, ErrorEventArgs e)
{
    //Any unhandled exception on the worker thread will be automatically marshaled here.
    MessageBox.Show(e.GetException().Message, "Error during send");
}
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
    'Send a message to the specified recipient
    'The Start method is used to execute the operation on a worker thread
    '(Event handlers were added in the designer)
    smtp1.Start(AddressOf sendMail, "myRecipient@dart.com")
End Sub

Private Sub sendMail(ByVal state As Object)
    'This function executes on a worker thread without blocking the UI
    'Create message to send
    Dim message As New MailMessage()
    message.To = TryCast(state, String)
    message.From = "sender@dart.com"
    message.Subject = "Here is your file"
    message.Text = "Please see the attached file."
    message.Attachments.Add(New Attachment("myAttachment.doc"))

    'Set session parameters
    smtp1.Session.RemoteEndPoint = New Dart.Mail.IPEndPoint("myMailServer", Smtp.GetDefaultPort(smtp1.Session))
    smtp1.Session.Username = "myUsername"
    smtp1.Session.Password = "myPassword"

    'Send message and logout
    smtp1.Send(message)
    smtp1.Close()
End Sub

Private Sub smtp1_Connection_Log(ByVal sender As Object, ByVal e As DataEventArgs)
    'Log the communication to a textbox
    textBox1.AppendText(e.Data.ToString())
End Sub

Private Sub smtp1_Error(ByVal sender As Object, ByVal e As ErrorEventArgs)
    'Any unhandled exception on the worker thread will be automatically marshaled here.
    MessageBox.Show(e.GetException().Message, "Error during send")
End Sub
See Also

Reference

MailBase Class
MailBase Members


PowerTCP Mail for .NET Documentation Version 4.3
© 2018 Dart Communications. All Rights Reserved.
Send comments on this topic