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

  • SEARCH
  • Home
  • 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 802669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:38:31+00:00 2026-05-14T23:38:31+00:00

The following code is part of a WCF service. Will eventWatcher take up a

  • 0

The following code is part of a WCF service. Will eventWatcher take up a thread in the ASP .NET thread pool, even if it is set IsBackground = true?

/// <summary>
/// Provides methods to work with the PhoneSystem web services SDK.
/// This is a singleton since we need to keep track of what lines (extensions) are open.
/// </summary>
public sealed class PhoneSystemWebServiceFactory : IDisposable
{
    // singleton instance reference
    private static readonly PhoneSystemWebServiceFactory instance = new PhoneSystemWebServiceFactory();
    private static readonly object l = new object();
    private static volatile Hashtable monitoredExtensions = new Hashtable();
    private static readonly PhoneSystemWebServiceClient webServiceClient = CreateWebServiceClient();
    private static volatile bool isClientRegistered;
    private static volatile string clientHandle;
    private static readonly Thread eventWatcherThread = new Thread(EventPoller) {IsBackground = true};

    #region Constructor
    // these constructors are hacks to make the C# compiler not mark beforefieldinit
    //  more info: http://www.yoda.arachsys.com/csharp/singleton.html
    static PhoneSystemWebServiceFactory()
    {
    }

    PhoneSystemWebServiceFactory()
    {
    }
    #endregion

    #region Properties
    /// <summary>
    /// Gets a thread safe instance of PhoneSystemWebServiceFactory
    /// </summary>
    public static PhoneSystemWebServiceFactory Instance
    {
        get { return instance; }
    }
    #endregion

    #region Private methods
    /// <summary>
    /// Create and configure a PhoneSystemWebServiceClient with basic http binding and endpoint from app settings.
    /// </summary>
    /// <returns>PhoneSystemWebServiceClient</returns>
    private static PhoneSystemWebServiceClient CreateWebServiceClient()
    {
        string url = ConfigurationManager.AppSettings["PhoneSystemWebService_Url"];
        if (string.IsNullOrEmpty(url))
        {
            throw new ConfigurationErrorsException(
                "The AppSetting \"PhoneSystemWebService_Url\" could not be found. Check the application configuration and ensure that the element exists. Example: <appSettings><add key=\"PhoneSystemWebService_Url\" value=\"http://xyz\" /></appSettings>");
        }

        return new PhoneSystemWebServiceClient(new BasicHttpBinding(), new EndpointAddress(url));
    }
    #endregion

    #region Event poller
    public static void EventPoller()
    {
        while (true)
        {
            if (Thread.CurrentThread.ThreadState == ThreadState.Aborted ||
                Thread.CurrentThread.ThreadState == ThreadState.AbortRequested ||
                Thread.CurrentThread.ThreadState == ThreadState.Stopped ||
                Thread.CurrentThread.ThreadState == ThreadState.StopRequested)
                break;

            // get events
            //webServiceClient.GetEvents(clientHandle, 30, 100);
        }

        Thread.Sleep(5000);
    }
    #endregion

    #region Client registration methods
    private static void RegisterClientIfNeeded()
    {
        if (isClientRegistered)
        {
            return;
        }

        lock (l)
        {
            // double lock check
            if (isClientRegistered)
            {
                return;
            }

            //clientHandle = webServiceClient.RegisterClient("PhoneSystemWebServiceFactoryInternal", null);
            isClientRegistered = true;
        }
    }

    private static void  UnregisterClient()
    {
        if (!isClientRegistered)
        {
            return;
        }

        lock (l)
        {
            // double lock check
            if (!isClientRegistered)
            {
                return;
            }

            //webServiceClient.UnegisterClient(clientHandle);
        }
    }
    #endregion

    #region Phone extension methods
    public bool SubscribeToEventsForExtension(string extension)
    {
        if (monitoredExtensions.Contains(extension))
        {
            return false;
        }

        lock (monitoredExtensions.SyncRoot)
        {
            // double lock check
            if (monitoredExtensions.Contains(extension))
            {
                return false;
            }

            RegisterClientIfNeeded();

            // open line so we receive events for extension
            LineInfo lineInfo;
            try
            {
                //lineInfo = webServiceClient.OpenLine(clientHandle, extension);
            }
            catch (FaultException<PhoneSystemWebSDKErrorDetail>)
            {
                // TODO: log error
                return false;
            }

            // add extension to list of monitored extensions
            //monitoredExtensions.Add(extension, lineInfo.lineID);
            monitoredExtensions.Add(extension, 1);

            // start event poller thread if not already started
            if (eventWatcherThread.ThreadState == ThreadState.Stopped || eventWatcherThread.ThreadState == ThreadState.Unstarted)
            {
                eventWatcherThread.Start();
            }

            return true;
        }
    }

    public bool UnsubscribeFromEventsForExtension(string extension)
    {
        if (!monitoredExtensions.Contains(extension))
        {
            return false;
        }

        lock (monitoredExtensions.SyncRoot)
        {
            if (!monitoredExtensions.Contains(extension))
            {
                return false;
            }

            // close line
            try
            {
                //webServiceClient.CloseLine(clientHandle, (int) monitoredExtensions[extension]);
            }
            catch (FaultException<PhoneSystemWebSDKErrorDetail>)
            {
                // TODO: log error
                return false;
            }

            // remove extension from list of monitored extensions
            monitoredExtensions.Remove(extension);

            // if we are not monitoring anything else, stop the poller and unregister the client
            if (monitoredExtensions.Count == 0)
            {
                eventWatcherThread.Abort();
                UnregisterClient();
            }

            return true;
        }
    }

    public bool IsExtensionMonitored(string extension)
    {
        lock (monitoredExtensions.SyncRoot)
        {
            return monitoredExtensions.Contains(extension);
        }
    }
    #endregion

    #region Dispose
    public void Dispose()
    {
        lock (l)
        {
            // close any open lines
            var extensions = monitoredExtensions.Keys.Cast<string>().ToList();

            while (extensions.Count > 0)
            {
                UnsubscribeFromEventsForExtension(extensions[0]);
                extensions.RemoveAt(0);
            }

            if (!isClientRegistered)
            {
                return;
            }

            // unregister web service client
            UnregisterClient();
        }
    }
    #endregion
}
  • 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-14T23:38:31+00:00Added an answer on May 14, 2026 at 11:38 pm

    No.

    Threads that you create using new Thread(...) (As opposed to ThreadPool.QueueUserWorkItem) have nothing to do with the ThreadPool, regardless of IsBackground.

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

Sidebar

Related Questions

I am using the following code as part of a set of scrollable bars
The following code is part of a PHP web-service I've written. It takes some
I am using the following code as part of an autocomplete script to avoid
Using linkedin-j , I have the following code in one part of my application
Ok - I have the following code - on the starred part I get
I have the following code in one cpp file which is part of a
The following code is taken from here . I removed all Windows NT part
I have the following code, which is run every 10ms as part of a
my problem is the following: This is a part of my HTML-Code: <form method='GET'
Google optimizer includes the following snippet as part of their conversion code. Unfortunately, the

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.