Semaphore and SemaphoreSlim usage Best Practices
<p>I have created a semaphore instance on top of this class</p>
<pre><code>public static SemaphoreSlim _zReportSemaphore = new SemaphoreSlim(1, 500);
</code></pre>
<p>And somewhere in my code i need to retrieve and send some data.</p>
<pre><code>while (_isRunning)
{
    try
    {
        xBsonDocument = null;
        //I think its very clear in this line...
        MongoDBDAO.xGetInstance().GetZReportData(ref xBsonDocument);

        foreach (BsonDocument item in xBsonDocument)
        {
            try
            {
                ThreadObject xThreadObject = new ThreadObject();
                xThreadObject.m_strTerminalId = item.GetValue(&quot;_id&quot;)[&quot;TERMINAL_ID&quot;].ToString();
                xThreadObject.m_strZNo = item.GetValue(&quot;_id&quot;)[&quot;Z_NO&quot;].ToString();

                m_xBuildAndSendZReportThread = 
                    new Thread(new ParameterizedThreadStart(vBuildAndSendZReport));
                m_xBuildAndSendZReportThread.Start(xThreadObject);
            }
            catch (Exception xException)
            {
                xException.TraceError();
                continue;
            }

            Thread.Sleep(m_litleStepQTime); 
        }
    }
    catch (Exception xException)
    {
        Thread.Sleep(m_bigStepQTime);
        Trace.vInsertError(xException);
        continue;
    }

    Thread.Sleep(m_iSleepTime);
}
</code></pre>
<p>This thread targeting to send files to ftp</p>
<pre><code>        private void vBuildAndSendZReport(object prm_objParameters)
        {
            _zReportSemaphore.Wait();
            RetriveDataFromMongoAndSend();
            _zReportSemaphore.Release();
        }
</code></pre>
<p>In this structure; if i don't use a semaphore it has working great but sometimes thread count overloading the CPU or Memory usage and machine has been crushing.</p>
<p>1- How can i provide control over data usage (ballancing, isolating threads  etc.) with this slim semaphore?</p>
<p>2- Can I use SemaphoreSlim  for this type of job in production? What can be the advantages and disadvantages of using such a workflow organization like this?
<strong>Does it improve performance?</strong> in my special case</p>
<p>3- Is there another alternative that will provide system resource management and will wrap up the technical exception management</p>
<p><strong>Update:</strong>
I asked this question during a job I did a long time ago. After solving the problem, I realized that I did not return.</p>
<p>In the above example, the report sending job was happening in the file sharing environment. Other solutions are possible, such as using a CDN.</p>
<p>The question was: Why should I use a thread if it can't keep me informed about what it's doing, if it doesn't tell me if it has had successful results? Why should I use SemaphoreSlim for example!?</p>
<p>yes, of course it can be done with async programming. but I didn't want to include this library in related environment. It had to be. I'm sure this situation is needed in many codes.</p>
<p>my solution was this: I eliminated the possibility of the exception in the code that was throwing the exception. so i synced the conflict with the thread outside the app. I made something like a threadpool. It worked flawlessly as a consumer. I did this by setting up a custom timing mechanism.</p>
<p>Regardless, I still agree. A thread should be set up to carry information about the job it is doing. I'm not talking about writing a Mutex object in between. Thread itself can carry this information.</p>
<p>By the way, I gave points to those who answered. Because they made the right comments according the question.</p>