Threading con .NET e OpenCV?
-
06-07-2019 - |
Domanda
Ho problemi a far funzionare un thread con OpenCV . Il problema è con la parte ThreadStart () del mio codice.
public ref class circles
{
public:
static void circleFind(bool isPhoto, const char * windowName1, const char * windowName2, const char * photoName)
{(stuff)}
};
int main(int argc, char* argv[])
{
const char *windowName1;
const char *windowName2;
const char *photoName;
windowName1 = "Find Circles";
windowName2 = "Gray";
photoName = "Colonies 3.jpg";
bool isPhoto = false;
//circles(isPhoto, windowName1, windowName2, photoName);
Thread^ circleThread = gcnew Thread(
gcnew ThreadStart (&circles::circleFind(isPhoto, windowName1, windowName2, photoName) ));
circleThread->Start();
area(isPhoto, photoName);
return 0;
}
Questo non è tutto il codice ma la parte importante. Gli errori che ottengo sono:
1 > .. \ .. \ .. \ .. \ .. \ .. \ .. \ Programmi (x86) \ OpenCV \ samples \ c \ circle dection.cpp (130): errore C2102: ' & amp;' richiede valore l
1 > .. \ .. \ .. \ .. \ .. \ .. \ .. \ Programmi (x86) \ OpenCV \ samples \ c \ circle dection.cpp (130): errore C3350: ' System :: Threading :: ThreadStart ': un costruttore delegato si aspetta 2 argomenti
Soluzione
Il problema qui è che stai provando a usare una funzione per un delegato ThreadStart
che ha una firma incompatibile. ThreadStart
è un delegato che non ha argomenti e non restituisce alcun valore. Stai tentando di utilizzare un metodo che accetta 4 argomenti. Questo non funzionerà.
Dovrai invece passare un metodo che non accetta argomenti.
Per passare i parametri in C ++, la soluzione migliore è creare una nuova classe che abbia tutti i parametri come campi. Quindi dagli un metodo che non ha parametri e non restituisce alcun valore e usalo come target ThreadStart
.
ThreadHelper^ h = gcnew ThreadHelper();
h->Param1 = someValue;
ThreadStart^ threadDelegate = gcnew ThreadStart( h, &ThreadHelper::DoMoreWork );
C'è un esempio completo di ciò nella pagina di documentazione ThreadStart