我正在Visual Studio 2017中使用C#。我有一个名为FileProcessor
的自定义类。
C#SemaphoreSlim
类具有名为CurrentCount
的属性,该属性获取可以输入SemaphoreSlim
对象的剩余线程数。
请记住,我的应用程序没有后端数据库,因此,我无法选择将值保存到数据库中。
有人可以向我解释如何通过SemaphoreSlim _sem
方法公开CurrentCount
实例的fileProcessorWcfServiceHost
值吗?再说一次,我没有该应用程序的后端数据库,因此我无法保持同步,将上述CurrentCount
保存到数据库中。
public class FileProcessor
{
private ServiceHost _fileProcessorWcfServiceHost;
private SemaphoreSlim _sem;
public Service(int maxConcurrentProcessesAllowed)
{
// Setup number of process
_sem = new SemaphoreSlim(maxConcurrentProcessesAllowed);
this._fileProcessorWcfServiceHost = new ServiceHost(typeof(InterCommunicationLibrary.MufgWcfService), new Uri("http://localhost:42031/MufgWcfService.svc"));
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
this._fileProcessorWcfServiceHost.Description.Behaviors.Add(smb);
}
private void ProcessNewFile(string fileName)
{
_sem.Wait();
try
{
..... blah blah code blah blah .....
}
catch (Exception x)
{
.... blah blah code blah blah ....
}
finally
{
.... blah blah code blah blah ....
// Finalize item
_sem.Release();
}
Console.WriteLine($"done processing {fileName}...");
}
}