简体   繁体   中英

Visual Studio 2015 Singleton issue

I have created ClassLibrary Project in VS2015. Inside I have created Singleton class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Library
{
    public class PoolOfThreads
    {
        private static PoolOfThreads instance = new PoolOfThreads();

        public static PoolOfThreads Instance
        {
            get { return instance; }
        }

        private List<Thread> listOfThreads;
        private int maxThreads;

        public int MaxThreads
        {
            get { return maxThreads; }
            set { maxThreads = value; }
        }

        private PoolOfThreads()
        {
            listOfThreads = new List<Thread>();
        }


        /// <exception cref="Exception"></exception>
        public Thread NewThread(ParameterizedThreadStart threadMethod)
        {
            FreeThreads();

            if (listOfThreads.Count < maxThreads)
            {
                Thread thread = new Thread(threadMethod);
                listOfThreads.Add(thread);
                return thread;
            }
            else
                throw new Exception("No free threads...");
        }

        private void FreeThreads()
        {
            /*var freeThreads = listOfThreads.Select(thread => thread.ThreadState == ThreadState.Stopped) as List<Thread>;
            foreach (Thread thread in freeThreads)
                listOfThreads.Remove(thread);
            */
            listOfThreads.RemoveAll(thread => thread.ThreadState == ThreadState.Stopped || thread.ThreadState == ThreadState.Aborted);
        }
    }
}

I have created also UnitTest Project. I added the reference to Library and everything is working properly. I can test my Singleton.

The problem is when I create Windows Form Appl. Every project is in the same solution. I added the references.

    using Library;
    using System.Net;
    using CalkaRozproszona.Classes;

    namespace CalkaRozproszona
    {
        public partial class ServerApplication : Form
        {
            Server server;
            ListObservator listObservator;
            PoolOfThreads.Instance.MaxThreads = 10;
            //Library.PoolOfThreads.Instance.MaxThreads = 10;


            public ServerApplication()
            {
                InitializeComponent();
            }
        }
     }

I cannot acces to my singleto class PoolOfThreads.在此处输入图片说明 . As you can see VS sees the class. But when I try to go into Instance property I get the error:在此处输入图片说明

Any ideas?

You can assign it to a variable like this:

public partial class ServerApplication : Form
{
    Library.PoolOfThreads poolOfThreads = Library.PoolOfThreads.Instance;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM