Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 881117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:12:31+00:00 2026-05-15T12:12:31+00:00

In one of Outlook add in, I have a worker thread that does some

  • 0

In one of Outlook add in, I have a worker thread that does some processing and then updates a boolean flag. Main thread checks this flag and if this is false , it just process a while loop and does nothing.

 
//worker thread 
void DoSoneThing()
{
Outlook.Recipients recps = mail.Recipients.
foreach(Outlook.Recipient recp in recps)
{
//Save each recipients in a colection
}
isDone=true;
}


//Main thread
while(!isDone)
{
//read the collection where recipients name have been stored.
}``

if the Main thread comes to this piece of code before the worker thread has set the flag to true, main thread keeps on processing the loop and secondry thread is just kind of paused. and since the isDone flag is never set to true, main thread doesn’t do any thing.

When I put a lock in the DoSomeThing method and used the same lock in mian thread, this issue is resolved.

myClass
{
public static object _syncRoot = new Object();
void DoSoneThing()
{
lock(_syncRoot)
{
//process 
isDone=true;
}
}
}

myOtherClass
{
    lock(myClass._syncRoot)
{
//process
}
}

My understanding was that lock is used to restrict the entry to same piece of code by more than one thread. But don’t understand why worker thread doesn’t do any thing when shared resource is accessed by main thread.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:12:32+00:00Added an answer on May 15, 2026 at 12:12 pm

    I think there is likely to be a slight conceptual problem here.

    Firstly can I suggest that the

    while(!isDone)
    

    is not a great way of waiting – It’s known as ‘spinning’ and allows the thread to use processor time when it’s not doing anything, which is not efficient. (Spinning on a lock can be ok in some specific circumstances, but in a user app is generally not a good plan.)

    The lock, making one thread wait while the other processes is much better.

    Now, as to your specific problem. It’s possible that the isDone flag read in the while test has been optimised out (i.e. the compiler ‘knows’ that it isn’t going to change, so it doesn’t put in any code to get it from memory again – it just tests the same CPU register.) You can overcome this by using the ‘volatile’ modifier to tell the compiler it must re-get the value from memory.
    It’s also possible that the main thread, spinning, is starving the other thread so it never gets a chance to set the flag (although one would hope it would eventually, in a ‘fair’ system.)

    Regardless of the latter, having a thread spin while waiting for another thread to finish proccessing is to be avoided, unless having checked the flag, it goes off to do something useful (such as updating the GUI) instead.

    You need to be very careful with multi-threaded designs – they’re intricate and can have obscure and hard-to-predict behaviour (but are well worth doing for the right circumstances.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My VSTO Outlook Add-in suddenly stopped working on one customer machine (it does not
I have 2 MSIs of an Outlook Add-in, one for Outlook 2007 and another
I want to install an outlook add-in for a user who does not have
We have written an add-on for Outlook that files emails into our CRM system.
I have created an outlook 2007 add-in project in vs2008, targeting .net 3.5, then
I have the following C# code to launch an outlook window. The one think
I'm building an Outlook 2010 add-in that will tie into our ticketing system using
I am having some issues creating a setup file for my Outlook 2007 add-in.
I am developing an add-in for Outlook. One requirement is to add a toolbar
When using the add-in wizard for outlook add-ins in Visual Studio 2008, I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.