Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 12th, 2006, 08:42 AM
Authorized User
 
Join Date: Apr 2006
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to vivekshah Send a message via Yahoo to vivekshah
Default How can i use semaphores in C#




While i am trying to use Semaphore in C#
& i am getting the following eror
<b>
The type or namespace name 'Semaphore' could not be found (are you missing a using directive or an assembly reference?)
</b>
What could be the solution for it.

giving the code snippet

<b>
// array of mutex to block the threads
        private Mutex[] m_mutex;

        // place holder to store thread to mutex mapping
        private Thread[] m_threadIds;

        // number of threads allowed to access the resources
        private int m_threadLimit;

        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.startThread.Click += new System.EventHandler(this.startThread_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void startThread_Click(object sender, System.EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(TestThread));
            t.Start();
        }
        public static void TestThread()
        {
            m_semaphore.Wait();
            MessageBox.Show("I am on a new thread");
            m_semaphore.Release();
        }

        public void Semaphore(int threadLimit)
        {
            m_threadLimit = threadLimit;
            m_mutex = new Mutex[m_threadLimit];
            m_threadIds = new Thread[m_threadLimit];
            for (int i=0; i<m_threadLimit; i++)
            {
                m_mutex[i] = new Mutex();
                m_threadIds[i] = null;
            }
        }

        public int Wait()
        {
            int index = WaitHandle.WaitAny(m_mutex);
            if (index != WaitHandle.WaitTimeout)
                m_threadIds[index] = Thread.CurrentThread;
            return index;
        }


        // release the mutex locked by the thread
        public void Release()
        {
            for (int i=0; i<m_threadLimits; i++)
            {
                if (m_threadIds[i] == Thread.CurrentThread)
                {
                    m_mutex[i].ReleaseMutex();
                    break;
                }
            }
        }
</b>


Vivek Shah
__________________
Vivek Shah
 
Old March 9th, 2008, 03:22 AM
Registered User
 
Join Date: Mar 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rrossenbg Send a message via Yahoo to rrossenbg
Default

I don't know what what you are up to,
but I'm sure if it starts, it would be
the best way to block the IIS.

 
Old March 10th, 2008, 09:34 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Vivek-

What are you trying to accomplish? There may be another way to do what you want. I worked with semaphores back in school in multi-threaded C++ but have never heard the term used in the context of .NET and/or C#. Perhaps there is something in the framework that will meet your needs.

-Peter
peterlanoie.blog
 
Old March 10th, 2008, 10:11 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Be aware - this thread is over a year old.

The Semaphore class was introduced into the .Net Framework in .Net 2.0:
http://msdn2.microsoft.com/en-us/lib...semaphore.aspx

The error : "The type or namespace name 'X' could not be found (are you missing a using directive or an assembly reference?)" usually means what it says, that either you haven't added a reference to a DLL to your project, or you are missing a 'using' (C#) or 'Imports' (VB.Net) statement.

/- Sam Judson : Wrox Technical Editor -/
 
Old March 10th, 2008, 10:18 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Sam,

Thanks for the clarification. I just skimmed over the doc you linked to. Am I right in understanding that the use of the .NET semaphore seems fairly different from the idea/use of a semaphore in multi threaded C++? I did MultiC in college (~10 years ago) and hazily remember semaphores to be signaling mechanism (akin to their namesake) possibly similar to a .NET reset event.

-Peter
peterlanoie.blog
 
Old March 10th, 2008, 10:31 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I'm not familiar with the C++ version of semaphores. The .Net Semaphore class seems to act like a kind of OS based thread pool, meaning that only a certain number of threads can access a specific resource, the rest being locked/queued until other threads release their locks.

They can also be used for simple in-process work, but the performance hit means that the Monitor class would be much better used in this scenario.

Unfortunately I've never used the Semaphore class in anger.

The Wikipedia page on semaphores appears to indicate that's what they do in other programming languages as well:
http://en.wikipedia.org/wiki/Semaphore_(programming)

/- Sam Judson : Wrox Technical Editor -/
 
Old March 10th, 2008, 10:44 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Agreed. After my post I wandered off to the same wiki article. It seems my memory is a little foggier than I thought. I had a class in real-time interfacing, part of which was doing C++ based simulation of RLL controllers. We started with a class library that used semaphores, but it seems we used them in a binary fashion for simple single value thread blocking/signaling needs (mutex).

-Peter
peterlanoie.blog
 
Old March 15th, 2008, 04:52 AM
yulin11
Guest
 
Posts: n/a
Default

yes,you can!

everyone can go far!www.codeuu.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Semaphores casselj Pro VB 6 1 May 5th, 2004 04:08 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.