establecer la prioridad para el bucle Parallel.For
Ok, aquí está la situación: mi hilo principal / UI (llamarlo Thread1) se usa para adquirir un lote de imágenes de un escáner de documentos físico. Cuando se adquiere un lote, se inicia un subproceso de "fondo" separado (llámelo Thread2) para procesar y guardar las imágenes de ese lote.
Thread2 (el hilo "de fondo") está utilizando unParallel.For
bucle que reduce el tiempo de procesamiento / ahorro de la imagen en un 70% sobre un normalFor
lazo. Sin embargo, también parece estar maximizando todos mis procesadores para que Thread1 no pueda comenzar a adquirir más imágenes hasta queParallel.For
el bucle se completa.
¿Hay una manera de "limitar" unParallel.For
bucle para que no maximice mis procesadores? O para establecer la prioridad de procesamiento? Intenté establecerThread2.Priority = ThreadPriority.Lowest
, pero esto no parece afectar el bucle. ¿O estoy malentendido cómo unParallel.For
funciona el bucle? ¿Está bloqueando Thread1 de alguna manera?
Así es como llamo a Thread2 desde un método en Thread1.
public void SaveWithSettings(bool save) // method in Thread1
{
....
Thread thr = new Thread(ThreadWork); // creating new thread (Thread 2)
thr.Priority = ThreadPriority.Lowest; // does nothing?
thr.Start(new SaveContainer(sc)); // pass a copy as paramater
// misc stuff to make scanning possible again
numBgw++;
twain.RemoveAllImages(); // clear images
imagelist.Clear(); // clear imagelist images
.... // etc. this all appears to process fine while Thread2 is processing
}
Aquí está miThreadWork
método:
private void ThreadWork(object data) // executing in Thread2
{
SaveContainer sc = data as SaveContainer; // holds images
bool[] blankIndex = new bool[sc.imagelist.Count]; // to use in Parallel.For loop
for (int i = 0; i < sc.imagelist.Count; i++)
blankIndex[i] = false; // set default value to false (not blank)
Parallel.For(0, sc.imagelist.Count, i => // loop to mark blank images
{
bool x = false; // local vars make loop more efficient
x = sc.IsBlankImage((short)i); // check if image at index i is blank
blankIndex[i] = x; // set if image is blank
}
.... // other image processing steps
}