Digital Powers
Digital Powers

Reputation: 470

Naming threads when using Reactive Extensions

I use multitasking a lot and I am working on adding Reactive Extensions into my set of tools. However I prefer to name threads so that I can better understand what threads are running when I break etc. In general it is just better. However I haven't found a good way to name threads when I am using reactive extensions with SubscribeOn and ObserveOn etc.

Any ideas?

Upvotes: 3

Views: 215

Answers (2)

Brandon
Brandon

Reputation: 39202

If you are using the NewThreadScheduler alot, you can make a subclass of it that customizes the Thread it creates and then use your custom scheduler when using ObserveOn or SubscribeOn...

// created threads will have names prefixed with "bob"
someObservable.ObserveOn(new CustomNewThreadScheduler("bob"))...

Upvotes: 4

Reed Copsey
Reed Copsey

Reputation: 564461

When using Rx, most of the "threads" fall into three camps:

  1. The target of SubscribeOn or ObserveOn, which will be based on the framework's specified SynchronizationContext. This is going to typically be the "main UI" thread if you're using a user interface, so naming the main thread will provide you the proper name.
  2. ThreadPool threads, which really cannot (or at least typically should not) be named, as they don't have a specific lifecycle which appears and goes away. If an event is raised on a ThreadPool thread, and being observed, there is no good way to "name" these threads.
  3. A thread you've created yourself, which could easily be named if you so choose.

Since 2/3 of the options allow you to provide names, you could always name those threads, then assume any unnamed thread watched during debugging is a ThreadPool managed thread.

Upvotes: 1

Related Questions