Sto usando un thread in background per inizializzare uno strumento via USB. L'interfaccia utente si blocca quando provo ad aprire il dispositivo. Mi aspetto che il thread in background si interrompa quando chiama Open sul dispositivo, ma non il thread dell'interfaccia utente. Sto testando questo senza interazione dell'interfaccia utente dal thread in background. Non so come risolvere il problema, ed è una domanda troppo ampia, ma forse qualcuno ha già visto qualcosa di simile prima. Non c'è niente di sbagliato con l'interoperabilità ActiveX, per quanto ne so, il dispositivo funziona correttamente. Questo è l'approccio generale:Come può un thread in background bloccare il thread dell'interfaccia utente?
using System;
using FancyVoltmeterLibrary;
namespace SOQuestion
{
public class MeterClass
{
private FancyVoltmeter meter;
private Thread meterThread;
public MeterClass()
{
// Create instance of ActiveX/COM object.
meter = new FancyVoltmeter();
meterThread = new Thread(UpdateMeter);
meterThread.Name = "Meter Thread";
meterThread.Priority = ThreadPriority.Normal;
meterThread.IsBackground = true;
meterThread.Start();
}
private void UpdateMeter()
{
while(true)
{
Thread.Sleep(1000);
if(!meter.IsOpen())
{
// Meter may be powered off here.
// The call to Open takes about 1 second.
// UI hangs during the call???
meter.Open();
}
// code to read meter goes here.
}
}
}
}
Modifica: Forse non chiaro cosa intendevo. Con "appendere" dovrei dire "si blocca momentaneamente".
L'interfaccia utente si blocca solo per un secondo e non so come rompere nel debugger in questo caso. Ma mi hai dato l'idea di inserire la "nuova riga FancyVoltmeter()" nel gestore di thread "UpdateMeter". L'interfaccia utente non si blocca più e questo è molto interessante. –