set priority for Parallel.For loop

前端 未结 5 1638
半阙折子戏
半阙折子戏 2021-01-05 16:21

Ok, here\'s the situation: My main/UI thread (call it Thread1) is used for acquiring a batch of images from a phsycial document scanner. When a batch has been acquired, a se

5条回答
  •  旧巷少年郎
    2021-01-05 16:53

    OK, I figured it out! I'm only posting this in case someone ever inadvertantly has this happen to them...

    It turns out that the Parallel.For thread was NOT blocking Thread1 (yes, you were all right). HOWEVER, an object in Thread1 was trying to grab a new Thread from the ThreadPool while the loop was crunching away, and thus the "delay" occured. I'm using a 3rd party SDK that allows me to interact with the TWAIN interface and there was an option ScanInNewThread = true that was attempting to grab a new thread each time the user started a new scan (which was occuring while the loop was crunching away). I was able to change this so that a single (but still separate) thread is used throughout an application session instead of grabbing a new thread for each scan batch, and BANG, no more noticeable delay.

    SO - the moral of the story:

    Existing threads should still function "normally" (with the exception of the thread calling the Parallel.For loop) as long as they are not trying to grab more threads from the ThreadPool while the loop is going.

提交回复
热议问题