473,326 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Threading guru required!

I am trying to write my first program using threading..basically I am
moving messages from an Outlook inbox and want to show the user where
the process is up to without having to wait until it has finished. I am
trying to follow this example :
http://www.codeproject.com/cs/miscct...ressdialog.asp

But although the messages still get moved, the progress window never
does anything. Here is my code in full, if anybody who knows anything
about threading could help me, it would be much appreciated!

Form1.cs :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Outlook;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}

private void btnGo_Click(object sender, EventArgs e)
{
ProgressWindow progress = new ProgressWindow();
progress.Text = "Work";
System.Threading.ThreadPool.QueueUserWorkItem(new
System.Threading.WaitCallback(DoSomeWork), progress);
progress.ShowDialog();
}

private void DoSomeWork(object status)
{
IProgressCallback callback = status as IProgressCallback;

try
{
Outlook.Application oApp = new
Outlook.ApplicationClass();

//Get MAPI namespace
Outlook.NameSpace oNS = oApp.GetNamespace("mapi");

string strUserName =
System.Environment.UserName.ToString();

try
{
oNS.Logon(strUserName,
System.Reflection.Missing.Value, false, true);
}
catch
{
throw new LogonException();
}

//Get Messages collection of Inbox
Outlook.MAPIFolder oInbox =
oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFo lderInbox);

try
{
Outlook.MAPIFolder destfolderTest =
oInbox.Folders.Item("Archive");
}
catch
{
throw new MissingFolderException();
}

Outlook.MAPIFolder destfolder =
oInbox.Folders.Item("Archive");

Outlook.Items oItems = oInbox.Items;

int intTotalNumberOfItemsInInbox = oItems.Count;

if (intTotalNumberOfItemsInInbox == 0)
throw new NoMessagesException();

//Get unread e-mail messages
//oItems = oItems.Restrict("[Unread] = true");
//int intTotalUnread = oItems.Count;

//DataTable dt = new DataTable();
//DataRow dr;

//dt.Columns.Add("ReceivedByName");
//dt.Columns.Add("SenderName");
//dt.Columns.Add("ReceivedTime");
//dt.Columns.Add("Subject");
//dt.Columns.Add("Body");

//Outlook.MailItem oMsg;
//int i;

//database connection
SqlConnection objConn = new SqlConnection();
objConn.ConnectionString = "server=e2bvsql4\\inst1; user
id=datawriter; password=rover9; database=SurveyResults";

try
{
objConn.Open();
}
catch
{
throw new DatabaseException();
}

//string strInsertQuery = "";

callback.Begin(0, intTotalNumberOfItemsInInbox);

for (int i = 0; i < oItems.Count; i++)
{
callback.SetText(String.Format("Performing op: {0}",
i));
callback.StepTo(i);

if (callback.IsAborting)
{
return;
}

oItems = oInbox.Items;
MoveMail(oItems, destfolder, objConn);

if (oItems.Count == 1)
{
i = -1;
}
else
{
i = 0;
}
}

objConn.Close();

//Loop through each unread message
//for (i = 1; i < intTotalNumberOfItemsInInbox + 1; i++)
//{
// //check if appointment or meeting request
// if (oItems.Item(i) is Outlook.MailItem)
// {
// oMsg = (Outlook.MailItem)oItems.Item(i);

//dr = dt.NewRow();
//dr["ReceivedByName"] = oMsg.ReceivedByName.ToString();
//dr["SenderName"] = oMsg.SenderName.ToString();
//dr["ReceivedTime"] = oMsg.ReceivedTime.ToString();
//dr["Subject"] = oMsg.Subject.ToString();
//dr["Body"] = oMsg.Body.ToString();
//dt.Rows.Add(dr);

////subject
//if ((oMsg.Subject == null) || (oMsg.Subject == ""))
//{
// oMsg.Subject = "blank";
//}
//else
//{
// //escape quotes in subject
// oMsg.Subject = oMsg.Subject.Replace("'", "''");
//}

//if (oMsg.ReceivedByName == null)
//{
// if (oMsg.SenderName.IndexOf("'") != -1)
// {
// string strSenderName =
oMsg.SenderName.Replace("'", "''");

// strInsertQuery = "INSERT INTO SeedingResults
(SenderName, ReceivedTime, Subject) VALUES ('" + strSenderName + "','" +
oMsg.ReceivedTime.ToString() + "','" + oMsg.Subject.ToString() + "')";
// }
// else
// {
// strInsertQuery = "INSERT INTO SeedingResults
(SenderName, ReceivedTime, Subject) VALUES ('" +
oMsg.SenderName.ToString() + "','" + oMsg.ReceivedTime.ToString() +
"','" + oMsg.Subject.ToString() + "')";
// }
//}
//else
//{
// if (oMsg.SenderName.IndexOf("'") != -1)
// {
// string strSenderName2 =
oMsg.SenderName.Replace("'", "''");

// strInsertQuery = "INSERT INTO SeedingResults
(ReceivedByName, SenderName, ReceivedTime, Subject) VALUES ('" +
oMsg.ReceivedByName.ToString() + "','" + strSenderName2 + "','" +
oMsg.ReceivedTime.ToString() + "','" + oMsg.Subject.ToString() + "')";
// }
// else
// {
// strInsertQuery = "INSERT INTO SeedingResults
(ReceivedByName, SenderName, ReceivedTime, Subject) VALUES ('" +
oMsg.ReceivedByName.ToString() + "','" + oMsg.SenderName.ToString() +
"','" + oMsg.ReceivedTime.ToString() + "','" + oMsg.Subject.ToString() +
"')";
// }
//}

////write to database
//SqlCommand objInsertCommand = new
SqlCommand(strInsertQuery, objConn);

//try
//{
//objConn.Open();

//objInsertCommand.ExecuteNonQuery();

//objConn.Close();
//}
//catch
//{
// throw new DatabaseException();
//}

//move record to archive folder
//oMsg.Move(destfolder);
// }
//}

//dataGridView1.DataSource = dt;
//GridView1.DataSource = dt;
//GridView1.DataBind();

//Log off
oNS.Logoff();

//Clean up
oApp = null;
oNS = null;
oItems = null;
//oMsg = null;

//inform user of success
lblStatus.ForeColor = System.Drawing.Color.Blue;
lblStatus.Text = "**Inbox messages successfully
transferred**";
}
catch (LogonException le)
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = "**Outlook Logon Error**";
}
catch (MissingFolderException mfe)
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = "**Please create Archive folder within
local Inbox folder**";
}
catch (NoMessagesException nme)
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = "**There are no messages in the local
Inbox to be transferred**";
}
catch (DatabaseException se)
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = "**Database Error**";
}
catch( System.Threading.ThreadAbortException )
{
// We want to exit gracefully here (if we're lucky)
}
catch( System.Threading.ThreadInterruptedException )
{
// And here, if we can
}
catch
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = "**General Exception**";
}
finally
{
if (callback != null)
{
callback.End();
}
}
}

private void MoveMail(Outlook.Items col, Outlook.MAPIFolder
destfolder, SqlConnection objConn)
{
string strInsertQuery = "";

for (int i = 1; i < col.Count + 1; i++)
{
if (col.Item(i) is Outlook.MailItem)
{
Outlook.MailItem oMsg =
(Outlook.MailItem)col.Item(i);

//subject
if ((oMsg.Subject == null) || (oMsg.Subject == ""))
{
oMsg.Subject = "blank";
}
else
{
//escape quotes in subject
oMsg.Subject = oMsg.Subject.Replace("'", "''");
}

if (oMsg.ReceivedByName == null)
{
if (oMsg.SenderName.IndexOf("'") != -1)
{
string strSenderName =
oMsg.SenderName.Replace("'", "''");

strInsertQuery = "INSERT INTO SeedingResults
(SenderName, ReceivedTime, Subject) VALUES ('" + strSenderName + "','" +
oMsg.ReceivedTime.ToString() + "','" + oMsg.Subject.ToString() + "')";
}
else
{
strInsertQuery = "INSERT INTO SeedingResults
(SenderName, ReceivedTime, Subject) VALUES ('" +
oMsg.SenderName.ToString() + "','" + oMsg.ReceivedTime.ToString() +
"','" + oMsg.Subject.ToString() + "')";
}
}
else
{
if (oMsg.SenderName.IndexOf("'") != -1)
{
string strSenderName2 =
oMsg.SenderName.Replace("'", "''");

strInsertQuery = "INSERT INTO SeedingResults
(ReceivedByName, SenderName, ReceivedTime, Subject) VALUES ('" +
oMsg.ReceivedByName.ToString() + "','" + strSenderName2 + "','" +
oMsg.ReceivedTime.ToString() + "','" + oMsg.Subject.ToString() + "')";
}
else
{
strInsertQuery = "INSERT INTO SeedingResults
(ReceivedByName, SenderName, ReceivedTime, Subject) VALUES ('" +
oMsg.ReceivedByName.ToString() + "','" + oMsg.SenderName.ToString() +
"','" + oMsg.ReceivedTime.ToString() + "','" + oMsg.Subject.ToString() +
"')";
}
}

//write to database
SqlCommand objInsertCommand = new
SqlCommand(strInsertQuery, objConn);

try
{
objInsertCommand.ExecuteNonQuery();
}
catch
{
throw new DatabaseException();
}

oMsg.Move(destfolder);
}
}
}
}
}

IProcessCallback.cs :

using System;

namespace WindowsApplication1
{
/// <summary>
/// This defines an interface which can be implemented by UI
elements
/// which indicate the progress of a long operation.
/// (See ProgressWindow for a typical implementation)
/// </summary>
public interface IProgressCallback
{
/// <summary>
/// Call this method from the worker thread to initialize
/// the progress callback.
/// </summary>
/// <param name="minimum">The minimum value in the progress
range (e.g. 0)</param>
/// <param name="maximum">The maximum value in the progress
range (e.g. 100)</param>
void Begin(int minimum, int maximum);

/// <summary>
/// Call this method from the worker thread to initialize
/// the progress callback, without setting the range
/// </summary>
void Begin();

/// <summary>
/// Call this method from the worker thread to reset the range
in the progress callback
/// </summary>
/// <param name="minimum">The minimum value in the progress
range (e.g. 0)</param>
/// <param name="maximum">The maximum value in the progress
range (e.g. 100)</param>
/// <remarks>You must have called one of the Begin() methods
prior to this call.</remarks>
void SetRange(int minimum, int maximum);

/// <summary>
/// Call this method from the worker thread to update the
progress text.
/// </summary>
/// <param name="text">The progress text to display</param>
/// <remarks>You must have called one of the Begin() methods
prior to this call.</remarks>
void SetText(String text);

/// <summary>
/// Call this method from the worker thread to increase the
progress counter by a specified value.
/// </summary>
/// <param name="val">The amount by which to increment the
progress indicator</param>
/// <remarks>You must have called one of the Begin() methods
prior to this call.</remarks>
void StepTo(int val);

/// <summary>
/// Call this method from the worker thread to step the progress
meter to a particular value.
/// </summary>
/// <param name="val">The value to which to step the
meter</param>
/// <remarks>You must have called one of the Begin() methods
prior to this call.</remarks>
void Increment(int val);

/// <summary>
/// If this property is true, then you should abort work
/// </summary>
/// <remarks>You must have called one of the Begin() methods
prior to this call.</remarks>
bool IsAborting
{
get;
}

/// <summary>
/// Call this method from the worker thread to finalize the
progress meter
/// </summary>
/// <remarks>You must have called one of the Begin() methods
prior to this call.</remarks>
void End();
}
}

ProgressWindow.cs :

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace MWA.Progress
{
/// <summary>
/// Summary description for ProgressWindow.
/// </summary>
public class ProgressWindow : System.Windows.Forms.Form,
IProgressCallback
{
private System.Windows.Forms.Button cancelButton;
private System.Windows.Forms.Label label;
private System.Windows.Forms.ProgressBar progressBar;

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public delegate void SetTextInvoker(String text);
public delegate void IncrementInvoker( int val );
public delegate void StepToInvoker( int val );
public delegate void RangeInvoker( int minimum, int maximum );

private String titleRoot = "";
private System.Threading.ManualResetEvent initEvent = new
System.Threading.ManualResetEvent(false);
private System.Threading.ManualResetEvent abortEvent = new
System.Threading.ManualResetEvent(false);
private bool requiresClose = true;

public ProgressWindow()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}

#region Implementation of IProgressCallback
/// <summary>
/// Call this method from the worker thread to initialize
/// the progress meter.
/// </summary>
/// <param name="minimum">The minimum value in the progress range
(e.g. 0)</param>
/// <param name="maximum">The maximum value in the progress range
(e.g. 100)</param>
public void Begin( int minimum, int maximum )
{
initEvent.WaitOne();
Invoke( new RangeInvoker( DoBegin ), new object[] { minimum, maximum
} );
}

/// <summary>
/// Call this method from the worker thread to initialize
/// the progress callback, without setting the range
/// </summary>
public void Begin()
{
initEvent.WaitOne();
Invoke( new MethodInvoker( DoBegin ) );
}

/// <summary>
/// Call this method from the worker thread to reset the range in the
progress callback
/// </summary>
/// <param name="minimum">The minimum value in the progress range
(e.g. 0)</param>
/// <param name="maximum">The maximum value in the progress range
(e.g. 100)</param>
/// <remarks>You must have called one of the Begin() methods prior to
this call.</remarks>
public void SetRange( int minimum, int maximum )
{
initEvent.WaitOne();
Invoke( new RangeInvoker( DoSetRange ), new object[] { minimum,
maximum } );
}

/// <summary>
/// Call this method from the worker thread to update the progress
text.
/// </summary>
/// <param name="text">The progress text to display</param>
public void SetText( String text )
{
Invoke( new SetTextInvoker(DoSetText), new object[] { text } );
}

/// <summary>
/// Call this method from the worker thread to increase the progress
counter by a specified value.
/// </summary>
/// <param name="val">The amount by which to increment the progress
indicator</param>
public void Increment( int val )
{
Invoke( new IncrementInvoker( DoIncrement ), new object[] { val } );
}

/// <summary>
/// Call this method from the worker thread to step the progress meter
to a particular value.
/// </summary>
/// <param name="val"></param>
public void StepTo( int val )
{
Invoke( new StepToInvoker( DoStepTo ), new object[] { val } );
}
/// <summary>
/// If this property is true, then you should abort work
/// </summary>
public bool IsAborting
{
get
{
return abortEvent.WaitOne( 0, false );
}
}

/// <summary>
/// Call this method from the worker thread to finalize the progress
meter
/// </summary>
public void End()
{
if( requiresClose )
{
Invoke( new MethodInvoker( DoEnd ) );
}
}
#endregion

#region Implementation members invoked on the owner thread
private void DoSetText( String text )
{
label.Text = text;
}

private void DoIncrement( int val )
{
progressBar.Increment( val );
UpdateStatusText();
}

private void DoStepTo( int val )
{
progressBar.Value = val;
UpdateStatusText();
}

private void DoBegin( int minimum, int maximum )
{
DoBegin();
DoSetRange( minimum, maximum );
}

private void DoBegin()
{
cancelButton.Enabled = true;
ControlBox = true;
}

private void DoSetRange( int minimum, int maximum )
{
progressBar.Minimum = minimum;
progressBar.Maximum = maximum;
progressBar.Value = minimum;
titleRoot = Text;
}

private void DoEnd()
{
Close();
}
#endregion

#region Overrides
/// <summary>
/// Handles the form load, and sets an event to ensure that
/// intialization is synchronized with the appearance of the form.
/// </summary>
/// <param name="e"></param>
protected override void OnLoad(System.EventArgs e)
{
base.OnLoad( e );
ControlBox = false;
initEvent.Set();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

/// <summary>
/// Handler for 'Close' clicking
/// </summary>
/// <param name="e"></param>
protected override void
OnClosing(System.ComponentModel.CancelEventArgs e)
{
requiresClose = false;
AbortWork();
base.OnClosing( e );
}
#endregion

#region Implementation Utilities
/// <summary>
/// Utility function that formats and updates the title bar text
/// </summary>
private void UpdateStatusText()
{
Text = titleRoot + String.Format( " - {0}% complete",
(progressBar.Value * 100 ) / (progressBar.Maximum - progressBar.Minimum)
);
}

/// <summary>
/// Utility function to terminate the thread
/// </summary>
private void AbortWork()
{
abortEvent.Set();
}
#endregion

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.progressBar = new System.Windows.Forms.ProgressBar();
this.label = new System.Windows.Forms.Label();
this.cancelButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// progressBar
//
this.progressBar.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.progressBar.Location = new System.Drawing.Point(8, 80);
this.progressBar.Name = "progressBar";
this.progressBar.Size = new System.Drawing.Size(192, 23);
this.progressBar.TabIndex = 1;
//
// label
//
this.label.Anchor = (((System.Windows.Forms.AnchorStyles.Top |
System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.label.Location = new System.Drawing.Point(8, 8);
this.label.Name = "label";
this.label.Size = new System.Drawing.Size(272, 64);
this.label.TabIndex = 0;
this.label.Text = "Starting operation...";
//
// cancelButton
//
this.cancelButton.Anchor = (System.Windows.Forms.AnchorStyles.Bottom
| System.Windows.Forms.AnchorStyles.Right);
this.cancelButton.DialogResult =
System.Windows.Forms.DialogResult.Cancel;
this.cancelButton.Enabled = false;
this.cancelButton.Location = new System.Drawing.Point(208, 80);
this.cancelButton.Name = "cancelButton";
this.cancelButton.TabIndex = 2;
this.cancelButton.Text = "Cancel";
//
// ProgressWindow
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(290, 114);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.cancelButton,
this.progressBar,
this.label});
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.Name = "ProgressWindow";
this.StartPosition =
System.Windows.Forms.FormStartPosition.CenterParen t;
this.Text = "ProgressWindow";
this.ResumeLayout(false);

}
#endregion
}
}


*** Sent via Developersdex http://www.developersdex.com ***
Mar 16 '07 #1
7 2332
Continued...

if (oMsg.SenderName.IndexOf("'") != -1)
// {
// string strSenderName2 =
oMsg.SenderName.Replace("'", "''");

// strInsertQuery = "INSERT INTO SeedingResults
(ReceivedByName, SenderName, ReceivedTime, Subject) VALUES ('" +
oMsg.ReceivedByName.ToString() + "','" + strSenderName2 + "','" +
oMsg.ReceivedTime.ToString() + "','" + oMsg.Subject.ToString() + "')";
// }
// else
// {
// strInsertQuery = "INSERT INTO SeedingResults
(ReceivedByName, SenderName, ReceivedTime, Subject) VALUES ('" +
oMsg.ReceivedByName.ToString() + "','" + oMsg.SenderName.ToString() +
"','" + oMsg.ReceivedTime.ToString() + "','" + oMsg.Subject.ToString() +
"')";
// }
//}

////write to database
//SqlCommand objInsertCommand = new
SqlCommand(strInsertQuery, objConn);

//try
//{
//objConn.Open();

//objInsertCommand.ExecuteNonQuery();

//objConn.Close();
//}
//catch
//{
// throw new DatabaseException();
//}

//move record to archive folder
//oMsg.Move(destfolder);
// }
//}

//dataGridView1.DataSource = dt;
//GridView1.DataSource = dt;
//GridView1.DataBind();

//Log off
oNS.Logoff();

//Clean up
oApp = null;
oNS = null;
oItems = null;
//oMsg = null;

//inform user of success
lblStatus.ForeColor = System.Drawing.Color.Blue;
lblStatus.Text = "**Inbox messages successfully
transferred**";
}
catch (LogonException le)
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = "**Outlook Logon Error**";
}
catch (MissingFolderException mfe)
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = "**Please create Archive folder within
local Inbox folder**";
}
catch (NoMessagesException nme)
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = "**There are no messages in the local
Inbox to be transferred**";
}
catch (DatabaseException se)
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = "**Database Error**";
}
catch( System.Threading.ThreadAbortException )
{
// We want to exit gracefully here (if we're lucky)
}
catch( System.Threading.ThreadInterruptedException )
{
// And here, if we can
}
catch
{
lblStatus.ForeColor = System.Drawing.Color.Red;
lblStatus.Text = "**General Exception**";
}
finally
{
if (callback != null)
{
callback.End();
}
}
}

private void MoveMail(Outlook.Items col, Outlook.MAPIFolder
destfolder, SqlConnection objConn)
{
string strInsertQuery = "";

for (int i = 1; i < col.Count + 1; i++)
{
if (col.Item(i) is Outlook.MailItem)
{
Outlook.MailItem oMsg =
(Outlook.MailItem)col.Item(i);

//subject
if ((oMsg.Subject == null) || (oMsg.Subject == ""))
{
oMsg.Subject = "blank";
}
else
{
//escape quotes in subject
oMsg.Subject = oMsg.Subject.Replace("'", "''");
}

if (oMsg.ReceivedByName == null)
{
if (oMsg.SenderName.IndexOf("'") != -1)
{
string strSenderName =
oMsg.SenderName.Replace("'", "''");

strInsertQuery = "INSERT INTO SeedingResults
(SenderName, ReceivedTime, Subject) VALUES ('" + strSenderName + "','" +
oMsg.ReceivedTime.ToString() + "','" + oMsg.Subject.ToString() + "')";
}
else
{
strInsertQuery = "INSERT INTO SeedingResults
(SenderName, ReceivedTime, Subject) VALUES ('" +
oMsg.SenderName.ToString() + "','" + oMsg.ReceivedTime.ToString() +
"','" + oMsg.Subject.ToString() + "')";
}
}
else
{
if (oMsg.SenderName.IndexOf("'") != -1)
{
string strSenderName2 =
oMsg.SenderName.Replace("'", "''");

strInsertQuery = "INSERT INTO SeedingResults
(ReceivedByName, SenderName, ReceivedTime, Subject) VALUES ('" +
oMsg.ReceivedByName.ToString() + "','" + strSenderName2 + "','" +
oMsg.ReceivedTime.ToString() + "','" + oMsg.Subject.ToString() + "')";
}
else
{
strInsertQuery = "INSERT INTO SeedingResults
(ReceivedByName, SenderName, ReceivedTime, Subject) VALUES ('" +
oMsg.ReceivedByName.ToString() + "','" + oMsg.SenderName.ToString() +
"','" + oMsg.ReceivedTime.ToString() + "','" + oMsg.Subject.ToString() +
"')";
}
}

//write to database
SqlCommand objInsertCommand = new
SqlCommand(strInsertQuery, objConn);

try
{
objInsertCommand.ExecuteNonQuery();
}
catch
{
throw new DatabaseException();
}

oMsg.Move(destfolder);
}
}
}
}
}

*** Sent via Developersdex http://www.developersdex.com ***
Mar 16 '07 #2
Continued...

IProcessCallback.cs :

using System;

namespace WindowsApplication1
{
/// <summary>
/// This defines an interface which can be implemented by UI
elements
/// which indicate the progress of a long operation.
/// (See ProgressWindow for a typical implementation)
/// </summary>
public interface IProgressCallback
{
/// <summary>
/// Call this method from the worker thread to initialize
/// the progress callback.
/// </summary>
/// <param name="minimum">The minimum value in the progress
range (e.g. 0)</param>
/// <param name="maximum">The maximum value in the progress
range (e.g. 100)</param>
void Begin(int minimum, int maximum);

/// <summary>
/// Call this method from the worker thread to initialize
/// the progress callback, without setting the range
/// </summary>
void Begin();

/// <summary>
/// Call this method from the worker thread to reset the range
in the progress callback
/// </summary>
/// <param name="minimum">The minimum value in the progress
range (e.g. 0)</param>
/// <param name="maximum">The maximum value in the progress
range (e.g. 100)</param>
/// <remarks>You must have called one of the Begin() methods
prior to this call.</remarks>
void SetRange(int minimum, int maximum);

/// <summary>
/// Call this method from the worker thread to update the
progress text.
/// </summary>
/// <param name="text">The progress text to display</param>
/// <remarks>You must have called one of the Begin() methods
prior to this call.</remarks>
void SetText(String text);

/// <summary>
/// Call this method from the worker thread to increase the
progress counter by a specified value.
/// </summary>
/// <param name="val">The amount by which to increment the
progress indicator</param>
/// <remarks>You must have called one of the Begin() methods
prior to this call.</remarks>
void StepTo(int val);

/// <summary>
/// Call this method from the worker thread to step the progress
meter to a particular value.
/// </summary>
/// <param name="val">The value to which to step the
meter</param>
/// <remarks>You must have called one of the Begin() methods
prior to this call.</remarks>
void Increment(int val);

/// <summary>
/// If this property is true, then you should abort work
/// </summary>
/// <remarks>You must have called one of the Begin() methods
prior to this call.</remarks>
bool IsAborting
{
get;
}

/// <summary>
/// Call this method from the worker thread to finalize the
progress meter
/// </summary>
/// <remarks>You must have called one of the Begin() methods
prior to this call.</remarks>
void End();
}
}
*** Sent via Developersdex http://www.developersdex.com ***
Mar 16 '07 #3
Continued...

And progressmenu.cs is also copied from the example code I mentioned.
*** Sent via Developersdex http://www.developersdex.com ***
Mar 16 '07 #4
IMO, that is a lot of code for a free forum... you may get takers, but
you'd do a *lot* better if you could demonstrate something simple (and
useful to you) but a bit shorter - e.g.

using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Threading;

class Program {

static void Main() {
Worker worker = new Worker();
using (Form f = new Form()) {
worker.ProgressChanged += delegate(object sender,
ProgressChangedEventArgs args) {
f.BeginInvoke((MethodInvoker)delegate { // switch back
to UI thread to update Text
f.Text = args.ProgressPercentage.ToString() + "%
complete";
});
};
f.Load += delegate { // start the worker on a background
thread at form load
ThreadPool.QueueUserWorkItem(worker.DoWork);
};
Application.Run(f);
}
}
}

class Worker {
public event ProgressChangedEventHandler ProgressChanged;
public void DoWork(object state) {
Random rand = new Random();
for (int i = 0; i < 100; i++) {
Thread.Sleep(rand.Next(750)); // simulate busy
OnProgressChanged(i, state);
}
}
protected void OnProgressChanged(int percent, object state) {
ProgressChangedEventHandler handler = ProgressChanged;
if (handler != null) handler(this, new
ProgressChangedEventArgs(percent, state));
}
}
Mar 16 '07 #5
Apologies, the only bits that are really relevany are in the first 2
posts, the rest are classes copied directly from the example I referred
to. The bit I am having problems with is here :

private void btnGo_Click(object sender, EventArgs e)
{
ProgressWindow progress = new ProgressWindow();
progress.Text = "Work";
System.Threading.ThreadPool.QueueUserWorkItem(new
System.Threading.WaitCallback(DoSomeWork), progress);
progress.ShowDialog();
}

private void DoSomeWork(object status)
{
IProgressCallback callback = status as IProgressCallback;

try
{
Outlook.Application oApp = new
Outlook.ApplicationClass();

//Get MAPI namespace
Outlook.NameSpace oNS = oApp.GetNamespace("mapi");

string strUserName =
System.Environment.UserName.ToString();

try
{
oNS.Logon(strUserName,
System.Reflection.Missing.Value, false, true);
}
catch
{
throw new LogonException();
}

//Get Messages collection of Inbox
Outlook.MAPIFolder oInbox =
oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFo lderInbox);

try
{
Outlook.MAPIFolder destfolderTest =
oInbox.Folders.Item("Archive");
}
catch
{
throw new MissingFolderException();
}

Outlook.MAPIFolder destfolder =
oInbox.Folders.Item("Archive");

Outlook.Items oItems = oInbox.Items;

int intTotalNumberOfItemsInInbox = oItems.Count;

if (intTotalNumberOfItemsInInbox == 0)
throw new NoMessagesException();

//Get unread e-mail messages
//oItems = oItems.Restrict("[Unread] = true");
//int intTotalUnread = oItems.Count;

//DataTable dt = new DataTable();
//DataRow dr;

//dt.Columns.Add("ReceivedByName");
//dt.Columns.Add("SenderName");
//dt.Columns.Add("ReceivedTime");
//dt.Columns.Add("Subject");
//dt.Columns.Add("Body");

//Outlook.MailItem oMsg;
//int i;

//database connection
SqlConnection objConn = new SqlConnection();
objConn.ConnectionString = "server=e2bvsql4\\inst1; user
id=datawriter; password=rover9; database=SurveyResults";

try
{
objConn.Open();
}
catch
{
throw new DatabaseException();
}

//string strInsertQuery = "";

callback.Begin(0, 2);

for (int i = 0; i < oItems.Count; i++)
{
callback.SetText(String.Format("Performing op: {0}",
i));
callback.StepTo(i);

if (callback.IsAborting)
{
return;
}

oItems = oInbox.Items;
MoveMail(oItems, destfolder, objConn);

if (oItems.Count == 1)
{
i = -1;
}
else
{
i = 0;
}
}

objConn.Close();

//Loop through each unread message
//for (i = 1; i < intTotalNumberOfItemsInInbox + 1; i++)
//{
// //check if appointment or meeting request
// if (oItems.Item(i) is Outlook.MailItem)
// {
// oMsg = (Outlook.MailItem)oItems.Item(i);

//dr = dt.NewRow();
//dr["ReceivedByName"] = oMsg.ReceivedByName.ToString();
//dr["SenderName"] = oMsg.SenderName.ToString();
//dr["ReceivedTime"] = oMsg.ReceivedTime.ToString();
//dr["Subject"] = oMsg.Subject.ToString();
//dr["Body"] = oMsg.Body.ToString();
//dt.Rows.Add(dr);

////subject
//if ((oMsg.Subject == null) || (oMsg.Subject == ""))
//{
// oMsg.Subject = "blank";
//}
//else
//{
// //escape quotes in subject
// oMsg.Subject = oMsg.Subject.Replace("'", "''");
//}

//if (oMsg.ReceivedByName == null)
//{
// if (oMsg.SenderName.IndexOf("'") != -1)
// {
// string strSenderName =
oMsg.SenderName.Replace("'", "''");

// strInsertQuery = "INSERT INTO SeedingResults
(SenderName, ReceivedTime, Subject) VALUES ('" + strSenderName + "','" +
oMsg.ReceivedTime.ToString() + "','" + oMsg.Subject.ToString() + "')";
// }
// else
// {
// strInsertQuery = "INSERT INTO SeedingResults
(SenderName, ReceivedTime, Subject) VALUES ('" +
oMsg.SenderName.ToString() + "','" + oMsg.ReceivedTime.ToString() +
"','" + oMsg.Subject.ToString() + "')";
// }
//}
//else
//{
// if (oMsg.SenderName.IndexOf("'") != -1)
// {
// string strSenderName2 =
oMsg.SenderName.Replace("'", "''");

// strInsertQuery = "INSERT INTO SeedingResults
(ReceivedByName, SenderName, ReceivedTime, Subject) VALUES ('" +
oMsg.ReceivedByName.ToString() + "','" + strSenderName2 + "','" +
oMsg.ReceivedTime.ToString() + "','" + oMsg.Subject.ToString() + "')";
// }
// else
// {
// strInsertQuery = "INSERT INTO SeedingResults
(ReceivedByName, SenderName, ReceivedTime, Subject) VALUES ('" +
oMsg.ReceivedByName.ToString() + "','" + oMsg.SenderName.ToString() +
"','" + oMsg.ReceivedTime.ToString() + "','" + oMsg.Subject.ToString() +
"')";
// }
//}

////write to database
//SqlCommand objInsertCommand = new
SqlCommand(strInsertQuery, objConn);

//try
//{
//objConn.Open();

//objInsertCommand.ExecuteNonQuery();

//objConn.Close();
//}
//catch
//{
// throw new DatabaseException();
//}

//move record to archive folder
//oMsg.Move(destfolder);
// }
//}

//dataGridView1.DataSource = dt;
//GridView1.DataSource = dt;
//GridView1.DataBind();

if (callback.IsAborting)
{
return;
}

//Log off
oNS.Logoff();

//Clean up
oApp = null;
oNS = null;
oItems = null;
//oMsg = null;

//inform user of success
//lblStatus.ForeColor = System.Drawing.Color.Blue;
//lblStatus.Text = "**Inbox messages successfully
transferred**";
}
catch (LogonException le)
{
//lblStatus.ForeColor = System.Drawing.Color.Red;
//lblStatus.Text = "**Outlook Logon Error**";
}
catch (MissingFolderException mfe)
{
//lblStatus.ForeColor = System.Drawing.Color.Red;
//lblStatus.Text = "**Please create Archive folder
within local Inbox folder**";
}
catch (NoMessagesException nme)
{
//lblStatus.ForeColor = System.Drawing.Color.Red;
//lblStatus.Text = "**There are no messages in the local
Inbox to be transferred**";
}
catch (DatabaseException se)
{
//lblStatus.ForeColor = System.Drawing.Color.Red;
//lblStatus.Text = "**Database Error**";
}
catch( System.Threading.ThreadAbortException )
{
// We want to exit gracefully here (if we're lucky)
}
catch( System.Threading.ThreadInterruptedException )
{
// And here, if we can
}
catch
{
//lblStatus.ForeColor = System.Drawing.Color.Red;
//lblStatus.Text = "**General Exception**";
}
finally
{
if (callback != null)
{
callback.End();
}
}
}

private void MoveMail(Outlook.Items col, Outlook.MAPIFolder
destfolder, SqlConnection objConn)
{
string strInsertQuery = "";

for (int i = 1; i < col.Count + 1; i++)
{
if (col.Item(i) is Outlook.MailItem)
{
Outlook.MailItem oMsg =
(Outlook.MailItem)col.Item(i);

//subject
if ((oMsg.Subject == null) || (oMsg.Subject == ""))
{
oMsg.Subject = "blank";
}
else
{
//escape quotes in subject
oMsg.Subject = oMsg.Subject.Replace("'", "''");
}

if (oMsg.ReceivedByName == null)
{
if (oMsg.SenderName.IndexOf("'") != -1)
{
string strSenderName =
oMsg.SenderName.Replace("'", "''");

strInsertQuery = "INSERT INTO SeedingResults
(SenderName, ReceivedTime, Subject) VALUES ('" + strSenderName + "','" +
oMsg.ReceivedTime.ToString() + "','" + oMsg.Subject.ToString() + "')";
}
else
{
strInsertQuery = "INSERT INTO SeedingResults
(SenderName, ReceivedTime, Subject) VALUES ('" +
oMsg.SenderName.ToString() + "','" + oMsg.ReceivedTime.ToString() +
"','" + oMsg.Subject.ToString() + "')";
}
}
else
{
if (oMsg.SenderName.IndexOf("'") != -1)
{
string strSenderName2 =
oMsg.SenderName.Replace("'", "''");

strInsertQuery = "INSERT INTO SeedingResults
(ReceivedByName, SenderName, ReceivedTime, Subject) VALUES ('" +
oMsg.ReceivedByName.ToString() + "','" + strSenderName2 + "','" +
oMsg.ReceivedTime.ToString() + "','" + oMsg.Subject.ToString() + "')";
}
else
{
strInsertQuery = "INSERT INTO SeedingResults
(ReceivedByName, SenderName, ReceivedTime, Subject) VALUES ('" +
oMsg.ReceivedByName.ToString() + "','" + oMsg.SenderName.ToString() +
"','" + oMsg.ReceivedTime.ToString() + "','" + oMsg.Subject.ToString() +
"')";
}
}

//write to database
SqlCommand objInsertCommand = new
SqlCommand(strInsertQuery, objConn);

try
{
objInsertCommand.ExecuteNonQuery();
}
catch
{
throw new DatabaseException();
}

oMsg.Move(destfolder);
}
}
}

I'm not sure where to put the threading code as I have 2 loops that
perform the majority of the work.

*** Sent via Developersdex http://www.developersdex.com ***
Mar 16 '07 #6
On Mar 16, 1:36 pm, Mike P <mike.p...@gmail.comwrote:
Apologies, the only bits that are really relevany are in the first 2
posts, the rest are classes copied directly from the example I referred
to. The bit I am having problems with is here :
<snip>

That's still a lot of code to go through. It would be better to
present something as short as possible but still complete and
demonstrating the problem.

See http://pobox.com/~skeet/csharp/complete.html

Jon

Mar 16 '07 #7
Mike P <mi*******@gmail.comwrote:
I am trying to write my first program using threading..
In that case I'd strongly recommend against using a GUI at all.

My threading pages give mostly examples which don't have a GUI,
precisely because in WinForms there are extra rules you have to obey,
and it's useful to be able to learn the basics without those extra
rules.

See http://pobox.com/~skeet/csharp/threads

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 16 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Z D | last post by:
Hello, I'm having a strange problem that is probably due to my lack of understanding of how threading & COM Interop works in a WinForms.NET application. Here's the situation: I have a 3rd...
4
by: Phillip N Rounds | last post by:
I'm cursed by being a linear thinker. Anyway, I have an application (service) where I can't decide on the appropriate way to implement threading. I have a timer which periodically initiates...
3
by: chxant | last post by:
Hi, I tried a simple example of multi-threading, but it's not working. test() is never excecuted. What's wrong?? This is the code: using System; using System.Collections; using...
0
by: Colmeister | last post by:
I recently read Jason Clark's excellent article on Unhandled Exceptions (http://msdn.microsoft.com/msdnmag/issues/04/06/NET/default.aspx) and have attempted to incorporate the features he talks...
9
by: AdrianJMartin | last post by:
Hi all, I have a need for a STA thread from asp.net. I can create the thread and it runs fine. But when it is finished, the thread still 'hangs' arround. Visible only to the debugger..... I get...
6
by: hzgt9b | last post by:
Using VS 2003, .NET: I developed a windows application that performs several actions based on an input file. The application displays a progress bar as each action executes. Based on new...
14
by: Simon Verona | last post by:
I think I'm doing this wrong : I have a class with a public shared method such as follows: public shared sub myFunction dim frm as new myFrm dim t as new Threading.Thread(Addressof ...
24
by: Maric Michaud | last post by:
This is a recurrent problem I encounter when I try to sell python solutions to my customers. I'm aware that this problem is sometimes overlooked, but here is the market's law. I've heard of a...
3
by: Barkingmadscot | last post by:
I am having problems with Multi-threading and write to Key Access Database I have a log file that and collecting infomation from getting the info is a problem or is writing it the database,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.