c# - Background worker issue with bluetooth socket -
i fixing bug in wpf application. application requires reading data bluetooth socket 32 feets library. current issue have when backgroundworker has been cancelled using cancelasync() , wait after few minutes, dowork event doesn't fire anymore. if stop backgroundworker within minute, dowork fire. know reason , how resolve this?
here code use dowork:
void startthread_dowork(object sender, doworkeventargs e) { backgroundworker worker = sender backgroundworker; try{ using (bluetoothclient cliente = new bluetoothclient()) { // bluetoothendpoint point = new bluetoothendpoint(bluetoothaddress.parse((string)e.argument), bluetoothservice.serialport); cliente.connect(bluetoothaddress.parse((string)e.argument), bluetoothservice.serialport); using (stream stream = cliente.getstream()) { while (true) { system.windows.forms.application.doevents(); if (worker.cancellationpending && !isactive) { e.cancel = true; break; } else { if (stream.read(data, 0, 8) != -1 && isactive) { decoder.decode(data); } } } } } } catch { } }
the code of constructing backgroundworker in constructor:
startthread = new backgroundworker(); startthread.workersupportscancellation = true; startthread.dowork += startthread_dowork;
thanks in advance.
Comments
Post a Comment