I'm in a situation where I have a particular sequence of operations that need to occur on the SAME THREAD, but the operations are a mix of Tasks, Observables, and various random code and method calls, including a try/catch. The kicker is it all needs to be executed on the same thread. For example...can you solve for this by rewriting the ExampleSequence method?:
void SomeVoidMethod() { /* misc logic here */ }
void SomeOtherVoidMethod() { /* misc logic here */ }
void SomeExceptionHandlingMethod() { /* misc logic here */ }
IObservable<int> SomeObservableProducingMethod()
{
/* something interesting here */
return Observable.Return(3);
}
IObservable<Unit> SomeOtherObservableProducingMethod()
{
/* something interesting here */
return Observable.Return(Unit.Default);
}
async Task ExampleSequence(Task someTask)
{
try
{
SomeVoidMethod();
await someTask;
var val = await SomeObservableProducingMethod().ToTask();
if (val > 0)
await SomeObservableProducingMethod().ToTask();
SomeOtherVoidMethod();
}
catch (Exception)
{
SomeExceptionHandlingMethod();
}
}
EDIT: What's the underlying problem (you may be wondering)? I have some limitations on what existing code can be changed, and right now when working against a SQlite database the 2nd database operation is failing (it never returns) once I try to wrap the operations in a transaction. My assumption has been that it is a thread locking issue as SQlite has pretty rudimentary ways of locking the database for transactions...
EDIT2: As per @Nksoi's link in the comments, https://stackoverflow.com/a/24195827/1735721, I tried using an AsyncLock. That didn't fix the hang but I discovered it's the very first Observable being awaited that ends up waiting forever. So in the example above it would be this line:
var val = await SomeObservableProducingMethod().ToTask();
Is there something I should be passing in to the Reactive Extension observable or the ToTask extension method to promote thread consistency? Also, now thinking that it could be something inside the Akavache library (https://github.com/akavache/Akavache) that is causing the problem. Akavache is producing the observables. I will do some more debugging...