Task.Factory.StartNew not executing the task when deployed

后端 未结 3 1950
余生分开走
余生分开走 2021-01-13 07:14

I have some code here that works as expected when I install it / run it on my own computer, Windows 7, but when I run it on other servers (2003 and 2008) it does not. The co

相关标签:
3条回答
  • 2021-01-13 07:49

    Do any log messages get printed in the log? Do you see "MonitorMainQueue called" get printed? How do you know the second Task is started but not the first? Could it be a permission issue with creating/writing to the log file?

    Edit: Additionally, in response to what @spender said about long running tasks, there is an overload to start the task with that option.

    Task.Factory.StartNew(MonitorMainQueue, TaskCreationOptions.LongRunning);

    0 讨论(0)
  • 2021-01-13 07:50

    I had the same issue when deploying to my production env.

    The issue was due to the identity used by the pool of the application.

    By default it was using applicationPoolIdentity which have restricted rights.

    We changed to "Network Service" and it worked.

    Nota : I'm not saying using NetWorkService is the best solution but it's cleraly a security right issue.

    0 讨论(0)
  • 2021-01-13 08:00

    Sometimes this kind of behaviour is an indication of an overloaded ThreadPool.

    Seeing as these are long running/blocking tasks, they should not be scheduled to run in the ThreadPool, which is where Task.Factory.StartNew will be sending them using the default TaskScheduler.

    IMO, Task.Factory.StartNew is probably not best suited to this, and you'd be better off spinning up your own threads to run these loops.

    ThreadStart action=()=>{
        //do your thing
    };
    Thread thread=new Thread(action){IsBackground=true};
    thread.Start();
    
    0 讨论(0)
提交回复
热议问题