È necessario eseguire questa operazione tramite ManualResetEvent.
ManualResetEvent mre = new ManualResetEvent();
mre.WaitOne(); // This will wait
su un altro thread, ovviamente, avrete bisogno di un riferimento all'istanza ManualResetEvent
.
mre.Set(); // Tells the other thread to go again
Un esempio completo che stamperà un testo, attendere che un altro thread di fare qualcosa e poi riprendere:
class Program
{
private static ManualResetEvent mre = new ManualResetEvent(false);
static void Main(string[] args)
{
Thread t = new Thread(new ThreadStart(SleepAndSet));
t.Start();
Console.WriteLine("Waiting");
mre.WaitOne();
Console.WriteLine("Resuming");
}
public static void SleepAndSet()
{
Thread.Sleep(2000);
mre.Set();
}
}
non ho mai fatto prima, ma sto pensando lungo le linee di bloccare il thread utilizzando un ManualResetEvent. –
@ByteBlast: Sembra un duplicato, tuttavia la risposta sull'altra domanda non è particolarmente buona:/ – Ian
Assicurati di farlo solo con un thread in background. Un thread dell'interfaccia utente (qualsiasi thread che si è unito a uno STA COM, in genere il thread principale e in genere qualsiasi thread che possiede una finestra per qualsiasi motivo) * deve * continuare a eseguire e pompare messaggi o causerà problemi di stabilità. –