있습니다 내 프로젝트 요구 사항이 완료 될 때 ConcurrentDictionary
- Windows 서비스.
- WCF 서비스는 많은 스레드를 시작하고 추적해야합니다.
- 각 스레드는 제공된 키로 식별 할 수 있어야합니다.
- 여러 가지 유형의 작업 (스레드)이 있으며 각 유형은 동일한 유형의 다른 작업이 완료 될 때까지 대기열의 추가 작업과 함께 "한 번에 최대 실행"제한이 있습니다. 내가 지금까지 무엇을 가지고
: (http://msdn.microsoft.com/en-us/library/ms733069(v=vs.110).aspx 유사) WCF 서비스를 호스팅
- Windows 서비스.
- 스레드가 포함 된 추상 클래스 (스레드를 확장 할 수 없기 때문에 컴포지션 사용)
- WCF 서비스 클래스에서 ConcurrentDictionary가 포함 된 MyThreadPool이라는 클래스의 정적 인스턴스가 실행중인 스레드의 레코드를 유지합니다.
내 질문은 :
- 이 (스레드의 완료에) 스레드 목록에서 완성 된 스레드를 제거하는 가장 좋은 방법은 무엇입니까?
- 이 시나리오에서 ConcurrentDictionary의 정적 인스턴스가 스레드를 관리하는 좋은 방법입니까? 그렇지 않다면 무엇을 권할 수 있습니까?
내 코드의 일부는 다음과 같습니다 : 그것은 모든 의미있는 작업을 수행 한 후
[ServiceContract(Namespace = "...")]
public interface IMyService
{
[OperationContract]
void StartProcess(int MIndexId, int MProcessId);
[OperationContract]
void StopProcess(int MIndexProcessId);
}
public class MyService : IMyService
{
private static MyThreadPool threadPool = new MyThreadPool();
public void StopProcess(int MIndexProcessId)
{
throw new NotImplementedException();
}
public void StartProcess(int ItemIdToProcess, int ProcessTypeId)
{
// call threadPool.LaunchThread(...)
}
}
public class MyThreadPool
{
private ConcurrentDictionary<int, BaseThread> _threads;
...
public void LaunchThread(BaseThread thread, int ItemIdToProcess)
{
// set additional data for thread (such as a key and name) for tracking in a database
_threads.AddOrUpdate(ItemIdToProcess, thread, (key, oldValue) => { return oldValue; });
thread.Start();
}
public void KillThread(int ItemIdToProcess)
{
...
}
}
public abstract class BaseThread
{
// some additional properties for tracking thread
// ...
private Thread _thread;
protected BaseThread()
{
_thread = new Thread(new ThreadStart(this.RunThread));
_thread.IsBackground = true;
}
// Thread methods/properties
public void Start() { _thread.Start(); }
public void Join() { _thread.Join(); }
public bool IsAlive { get { return _thread.IsAlive; } }
public string Name
{
get
{
return _thread.Name;
}
set
{
_thread.Name = value;
}
}
public void Abort()
{
_thread.Abort();
}
public abstract void RunThread();
}
public class ValidateThread : BaseThread
{
public override void RunThread()
{
...
// indicate to calling thread to remove from thread list();
}
}
귀하의 질문에 답변했습니다. 더 구체적으로해야 할 문제는 무엇입니까? – usr
일반적으로 WCF 스레딩 기능으로는 다루지 않는 특별한 요구 사항이없는 한 자체 ThreadPool을 작성하는 것은 좋지 않습니다. 이 질문에 대한 답변보기 : http://stackoverflow.com/questions/3295045/does-wcf-support-multi-threading-itself –