Force WCF to use one thread

后端 未结 3 1792
春和景丽
春和景丽 2021-01-14 06:27

I have a console application which uses an external library. The library insists on always being called from the same thread; it locks up otherwise. (I did try runn

3条回答
  •  不要未来只要你来
    2021-01-14 07:15

    Since ExecuteInWorkerThread runs all actions on one thread, it will block if called recursively. So I suspect your hang maybe because you have an action calling the library, which then calls the library again via ExecuteInWorkerThread before completing.

    I've not tested your ThreadManager class; my gut says it looks too complicated though, so as an aside, if you don't mind including Reactive Extensions (nuget package rx-main), then you can refactor your ThreadManager class like this:

    public class ThreadManager
    {
        EventLoopScheduler _scheduler = new EventLoopScheduler();    
    
        public T ExecuteInWorkerThread(Func action)
        {
            return Observable.Start(action, _scheduler).Wait();
        }
    }
    

    And you can add this method to it if you need an async call too:

    public Task ExecuteInWorkerThreadAsync(Func action)
    {
        return Observable.Start(action, _scheduler).ToTask();
    }
    

提交回复
热议问题