473,545 Members | 2,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

threading

I have two loops that I am running, one of which is calling a method
which contains the second one. What I want to happen is for when the
second loop exits (the inner loop), then the code will return to the
first loop (the outer loop) and continue executing, but this doesn't
seem to be happening. Here is my code if anybody can help me out :

Loop 1 :

for (int i = 0; i < oItems.Count; i++)
{
oItems = oInbox.Items;

ProgressWindow progress = new ProgressWindow( );
progress.Text = "Work";
System.Threadin g.ThreadPool.Qu eueUserWorkItem (new
System.Threadin g.WaitCallback( MoveMail), new object[] { progress,
oItems, destfolder, objConn });

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

progress.ShowDi alog();
}

Loop 2 in MoveMail method :

private void MoveMail(object status)
{
object[] parms = (object[])status;
IProgressCallba ck callback = (IProgressCallb ack)parms[0];
Outlook.Items col = (Outlook.Items) parms[1];
Outlook.MAPIFol der destfolder =
(Outlook.MAPIFo lder)parms[2];
SqlConnection objConn = (SqlConnection) parms[3];

string strInsertQuery = "";

callback.Begin( 0, col.Count);

for (int i = 1; i < col.Count + 1; i++)
{
callback.SetTex t(String.Format ("Performing op: {0}",
i));
callback.StepTo (i);

if (callback.IsAbo rting)
{
return;
}

if (col.Item(i) is Outlook.MailIte m)
{
Outlook.MailIte m oMsg =
(Outlook.MailIt em)col.Item(i);

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

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

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

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

//write to database
SqlCommand objInsertComman d = new
SqlCommand(strI nsertQuery, objConn);

try
{
objInsertComman d.ExecuteNonQue ry();
}
catch
{
throw new DatabaseExcepti on();
}

oMsg.Move(destf older);

if (callback.IsAbo rting)
{
return;
}
}
}
}

*** Sent via Developersdex http://www.developersdex.com ***
Mar 20 '07 #1
10 3700
On Mar 20, 12:40 pm, Mike P <mike.p...@gmai l.comwrote:
I have two loops that I am running, one of which is calling a method
which contains the second one. What I want to happen is for when the
second loop exits (the inner loop), then the code will return to the
first loop (the outer loop) and continue executing, but this doesn't
seem to be happening. Here is my code if anybody can help me out :
There's a lot of code there, most of it not relevant to the problem.

Could you post a short but complete program that demonstrates the
problem?
See http://pobox.com/~skeet/csharp/complete/html for more on what I
mean by this.

Jon

Mar 20 '07 #2
Here's the code cut down :

Loop 1 :

for (int i = 0; i < oItems.Count; i++)
{
oItems = oInbox.Items;

ProgressWindow progress = new ProgressWindow( );
progress.Text = "Work";
System.Threadin g.ThreadPool.Qu eueUserWorkItem (new
System.Threadin g.WaitCallback( MoveMail), new object[] { progress,
oItems, destfolder, objConn });

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

progress.ShowDi alog();
}

Loop 2 :

private void MoveMail(object status)
{
object[] parms = (object[])status;
IProgressCallba ck callback = (IProgressCallb ack)parms[0];
Outlook.Items col = (Outlook.Items) parms[1];
Outlook.MAPIFol der destfolder =
(Outlook.MAPIFo lder)parms[2];
SqlConnection objConn = (SqlConnection) parms[3];

string strInsertQuery = "";

callback.Begin( 0, col.Count);

for (int i = 1; i < col.Count + 1; i++)
{
callback.SetTex t(String.Format ("Performing op: {0}",
i));
callback.StepTo (i);

if (callback.IsAbo rting)
{
return;
}

if (col.Item(i) is Outlook.MailIte m)
{
Outlook.MailIte m oMsg =
(Outlook.MailIt em)col.Item(i);

//db stuff goes here

oMsg.Move(destf older);

if (callback.IsAbo rting)
{
return;
}
}
}
}
*** Sent via Developersdex http://www.developersdex.com ***
Mar 20 '07 #3
On Mar 20, 2:00 pm, Mike P <mike.p...@gmai l.comwrote:
Here's the code cut down :
<snip>

Please read http://www.yoda.arachsys.com/csharp/incomplete.html

Jon

Mar 20 '07 #4
Here's another attempt :

Main form :

Outlook.Applica tion oApp = new Outlook.Applica tionClass();

Outlook.NameSpa ce oNS = oApp.GetNamespa ce("mapi");

string strUserName =
System.Environm ent.UserName.To String();

oNS.Logon(strUs erName, System.Reflecti on.Missing.Valu e, false, true);

Outlook.MAPIFol der oInbox =
oNS.GetDefaultF older(Outlook.O lDefaultFolders .olFolderInbox) ;

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

Outlook.Items oItems = oInbox.Items;

int intTotalNumberO fItemsInInbox = oItems.Count;

for (int i = 0; i < oItems.Count; i++)
{
oItems = oInbox.Items;

ProgressWindow progress = new ProgressWindow( );
progress.Text = "Work";
System.Threadin g.ThreadPool.Qu eueUserWorkItem (new
System.Threadin g.WaitCallback( MoveMail), new object[] { progress,
oItems, destfolder, objConn });

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

progress.ShowDi alog();
}

private void MoveMail(object status)
{
object[] parms = (object[])status;
IProgressCallba ck callback = (IProgressCallb ack)parms[0];
Outlook.Items col = (Outlook.Items) parms[1];
Outlook.MAPIFol der destfolder =
(Outlook.MAPIFo lder)parms[2];
SqlConnection objConn = (SqlConnection) parms[3];

callback.Begin( 0, col.Count);

for (int i = 1; i < col.Count + 1; i++)
{
callback.SetTex t(String.Format ("Performing op: {0}",
i));
callback.StepTo (i);

if (callback.IsAbo rting)
{
return;
}

if (col.Item(i) is Outlook.MailIte m)
{
Outlook.MailIte m oMsg =
(Outlook.MailIt em)col.Item(i);

oMsg.Move(destf older);

if (callback.IsAbo rting)
{
return;
}
}
}
}

Added to this I am using ProgressWindow. cs and IProgressCallba ck.cs from
the following example :

http://www.codeproject.com/cs/miscct...ressdialog.asp

HTH,

Mike


*** Sent via Developersdex http://www.developersdex.com ***
Mar 20 '07 #5
The problem I have with this code is that without the threading code,
after it has completed executing inside the second loop it returns to
the first loop, whereas when the threading code is added, it doesn't.
Here's the code in it's original form :

for (int i = 0; i < oItems.Count; i++)
{
oItems = oInbox.Items;

MoveMail(oItems , destfolder, objConn);

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

private void MoveMail(Outloo k.Items col, Outlook.MAPIFol der destfolder,
SqlConnection objConn)
{

for (int i = 1; i < col.Count + 1; i++)
{
if (col.Item(i) is Outlook.MailIte m)
{
Outlook.MailIte m oMsg =
(Outlook.MailIt em)col.Item(i);

oMsg.Move(destf older);
}
}
}

*** Sent via Developersdex http://www.developersdex.com ***
Mar 20 '07 #6
On Mar 20, 2:23 pm, Mike P <mike.p...@gmai l.comwrote:
Here's another attempt :
*Please* read the link I gave.

Try to cut and paste the code you've posted directly into an empty
file and compile it. It won't work, because it's incomplete.

Also, anything which requires Outlook isn't going to be easy for
everyone to reproduce. Put in some dummy work (eg writing a counter to
the console). This isn't meant to be real code, it's meant to be code
which *just* demonstrates the problem.

Jon

Mar 20 '07 #7
"Mike P" <mi*******@gmai l.comwrote in message
news:ue******** ******@TK2MSFTN GP05.phx.gbl...
Here's another attempt :

Main form :

Outlook.Applica tion oApp = new Outlook.Applica tionClass();

Outlook.NameSpa ce oNS = oApp.GetNamespa ce("mapi");

string strUserName =
System.Environm ent.UserName.To String();

oNS.Logon(strUs erName, System.Reflecti on.Missing.Valu e, false, true);

Outlook.MAPIFol der oInbox =
oNS.GetDefaultF older(Outlook.O lDefaultFolders .olFolderInbox) ;

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

Outlook.Items oItems = oInbox.Items;

int intTotalNumberO fItemsInInbox = oItems.Count;

for (int i = 0; i < oItems.Count; i++)
{
oItems = oInbox.Items;

ProgressWindow progress = new ProgressWindow( );
progress.Text = "Work";
System.Threadin g.ThreadPool.Qu eueUserWorkItem (new
System.Threadin g.WaitCallback( MoveMail), new object[] { progress,
oItems, destfolder, objConn });

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

progress.ShowDi alog();
}

private void MoveMail(object status)
{
object[] parms = (object[])status;
IProgressCallba ck callback = (IProgressCallb ack)parms[0];
Outlook.Items col = (Outlook.Items) parms[1];
Outlook.MAPIFol der destfolder =
(Outlook.MAPIFo lder)parms[2];
SqlConnection objConn = (SqlConnection) parms[3];

callback.Begin( 0, col.Count);

for (int i = 1; i < col.Count + 1; i++)
{
callback.SetTex t(String.Format ("Performing op: {0}",
i));
callback.StepTo (i);

if (callback.IsAbo rting)
{
return;
}

if (col.Item(i) is Outlook.MailIte m)
{
Outlook.MailIte m oMsg =
(Outlook.MailIt em)col.Item(i);

oMsg.Move(destf older);

if (callback.IsAbo rting)
{
return;
}
}
}
}

Added to this I am using ProgressWindow. cs and IProgressCallba ck.cs from
the following example :

http://www.codeproject.com/cs/miscct...ressdialog.asp

HTH,

Mike


*** Sent via Developersdex http://www.developersdex.com ***


It's really hard to see what's going on, but I would suggest you to forget about a
threadpool thread, use a 'normal auxiliary' thread and initialize this one to enter the STA.
This way you are creating the COM reference on the main UI thread (STA) and you pass COM
references to a worker thread taken from the pool, however these pool threads are running in
the MTA, that means that you probably have to deal with marshaling issues between the MTA
and the STA thread.
Another option is to create the "Outlook" instance from the auxiliary thread, don't
initialize it to enter an STA, should work from MTA.

Willy.


Mar 20 '07 #8
Willy,

Apologies, I'm new to threading, but what do you mean by MTA?
Thanks,

Mike
*** Sent via Developersdex http://www.developersdex.com ***
Mar 21 '07 #9
Willy,

Apologies, I'm new to threading, but what do you mean by MTA?
Thanks,

Mike
*** Sent via Developersdex http://www.developersdex.com ***
Mar 21 '07 #10

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

Similar topics

65
6668
by: Anthony_Barker | last post by:
I have been reading a book about the evolution of the Basic programming language. The author states that Basic - particularly Microsoft's version is full of compromises which crept in along the language's 30+ year evolution. What to you think python largest compromises are? The three that come to my mind are significant whitespace,...
2
2959
by: Egor Bolonev | last post by:
hi all my program terminates with error i dont know why it tells 'TypeError: run() takes exactly 1 argument (10 given)' =program==================== import os, os.path, threading, sys def get_all_files(path): """return all files of folder path, scan with subfolders
77
5216
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for the moment. I'd be *very* grateful if people with any interest in multi-threading would read it (even just bits of it - it's somewhat long to go...
6
555
by: CK | last post by:
I have the following code in a windows service, when I start the windows service process1 and process2 work fine , but final process (3) doesnt get called. i stop and restart the windows service and the final process(3) gets called. what am I doing wrong with the threading? by the way Directory.GetFiles(IncomingXMLPath1).Length is some...
2
2235
by: Vjay77 | last post by:
In this code: Private Sub downloadBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) If Not (Me.downloadUrlTextBox.Text = "") Then Me.outputGroupBox.Enabled = True Me.bytesDownloadedTextBox.Text = "" Me.totalBytesTextBox.Text = ""
11
4998
by: Paul Sijben | last post by:
I am stumped by the following problem. I have a large multi-threaded server accepting communications on one UDP port (chosen for its supposed speed). I have been profiling the code and found that the UDP communication is my biggest drain on performance! Communication where the client and the server are on the same machine still takes 300ms...
17
6395
by: OlafMeding | last post by:
Below are 2 files that isolate the problem. Note, both programs hang (stop responding) with hyper-threading turned on (a BIOS setting), but work as expected with hyper-threading turned off. Note, the Windows task manager shows 2 CPUs on the Performance tab with hyper-threading is turned on. Both Python 2.3.5 and 2.4.3 (downloaded from...
0
1571
by: kingcrowbar.list | last post by:
Hello Everyone I have been playing a little with pyGTK and threading to come up with simple alert dialog which plays a sound in the background. The need for threading came when in the first version i made, the gui would freeze after clicking the close button until pygame finished playing the sound. In Windows it was acceptable because it...
7
2357
by: Mike P | last post by:
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/miscctrl/progressdialog.asp But although the messages still get...
126
6632
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard: http://www.cse.wustl.edu/~schmidt/ACE.html the same way that the STL (and subsequently BOOST) have been subsumed? Since it already runs on zillions of platforms, they have...
0
7935
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7449
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7780
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5351
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5069
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3479
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
734
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.