)} ,System.Boolean)} ,System.Object)} ,System.Reactive.Concurrency.AsyncLock)}
Observer.Synchronize(IObserver) method¶
Defined in
Type: Observer
Namespace: System.Reactive
Assembly: System.Reactive.dll
Applies to
netstandard2.0
Overloads¶
- 1.
public static IObserver<T> Synchronize<T>(IObserver<T> observer) - 2.
public static IObserver<T> Synchronize<T>(IObserver<T> observer, bool preventReentrancy) - 3.
public static IObserver<T> Synchronize<T>(IObserver<T> observer, object gate) - 4.
public static IObserver<T> Synchronize<T>(IObserver<T> observer, AsyncLock asyncLock)
1. Overload¶
public static IObserver<T> Synchronize<T>(IObserver<T> observer)
Summary: Synchronizes access to the observer such that its callback methods cannot be called concurrently from multiple threads. This overload is useful when coordinating access to an observer. Notice reentrant observer callbacks on the same thread are still possible.
Type parameters
| Name | Description |
|---|---|
T | The type of the elements received by the source observer. |
Parameters
| Name | Type | Description |
|---|---|---|
observer | IObserver | The observer whose callbacks should be synchronized. |
Returns: IObserver
Remarks
Because a Monitor is used to perform the synchronization, there's no protection against reentrancy from the same thread. Hence, overlapped observer callbacks are still possible, which is invalid behavior according to the observer grammar. In order to protect against this behavior as well, use the Synchronize overload, passing true for the second parameter.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | observer is null. |
2. Overload¶
public static IObserver<T> Synchronize<T>(IObserver<T> observer, bool preventReentrancy)
Summary:
Synchronizes access to the observer such that its callback methods cannot be called concurrently. This overload is useful when coordinating access to an observer.
The preventReentrancy parameter configures the type of lock used for synchronization.
Type parameters
| Name | Description |
|---|---|
T | The type of the elements received by the source observer. |
Parameters
| Name | Type | Description |
|---|---|---|
observer | IObserver | The observer whose callbacks should be synchronized. |
preventReentrancy | bool | If set to true, reentrant observer callbacks will be queued up and get delivered to the observer in a sequential manner. |
Returns: IObserver
Remarks
When the preventReentrancy parameter is set to false, behavior is identical to the Synchronize overload which uses
a Monitor for synchronization. When the preventReentrancy parameter is set to true, an AsyncLock
is used to queue up callbacks to the specified observer if a reentrant call is made.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | observer is null. |
3. Overload¶
public static IObserver<T> Synchronize<T>(IObserver<T> observer, object gate)
Summary: Synchronizes access to the observer such that its callback methods cannot be called concurrently by multiple threads, using the specified gate object for use by a Monitor-based lock. This overload is useful when coordinating multiple observers that access shared state by synchronizing on a common gate object. Notice reentrant observer callbacks on the same thread are still possible.
Type parameters
| Name | Description |
|---|---|
T | The type of the elements received by the source observer. |
Parameters
| Name | Type | Description |
|---|---|---|
observer | IObserver | The observer whose callbacks should be synchronized. |
gate | object | Gate object to synchronize each observer call on. |
Returns: IObserver
Remarks
Because a Monitor is used to perform the synchronization, there's no protection against reentrancy from the same thread. Hence, overlapped observer callbacks are still possible, which is invalid behavior according to the observer grammar. In order to protect against this behavior as well, use the Synchronize overload.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | observer or gate is null. |
4. Overload¶
public static IObserver<T> Synchronize<T>(IObserver<T> observer, AsyncLock asyncLock)
Summary: Synchronizes access to the observer such that its callback methods cannot be called concurrently, using the specified asynchronous lock to protect against concurrent and reentrant access. This overload is useful when coordinating multiple observers that access shared state by synchronizing on a common asynchronous lock.
Type parameters
| Name | Description |
|---|---|
T | The type of the elements received by the source observer. |
Parameters
| Name | Type | Description |
|---|---|---|
observer | IObserver | The observer whose callbacks should be synchronized. |
asyncLock | [AsyncLock](# | Gate object to synchronize each observer call on. |
Returns: IObserver
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | observer or asyncLock is null. |