> For the complete documentation index, see [llms.txt](https://docs.zongsoft.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.zongsoft.com/documentation/en/framework/core/components/superviser.md).

# Superviser

Monitor the lifecycle using the framework's SuperviserTest real-world objects and concurrent testing instructions.

Superviser manages monitored objects, searches and cancels monitoring by pressing keys, and notifies status changes through events. Discussions There is currently no monitor business process, so this page uses an existing use case for the framework SuperviserTest.

## The Meaning of Registration and Keys

Source: [framework/Zongsoft.Core/test/Components/SuperviserTest.cs](https://github.com/Zongsoft/framework/blob/main/Zongsoft.Core/test/Components/SuperviserTest.cs#L17) (excerpt; see source for context).

{% code title="SuperviserTest.cs" %}

```csharp
private void Initialize(int count = 2)
{
	for(int i = 0; i < count; i++)
	{
		var name = $"S{i + 1}";
		_superviser.Supervise(name, new MySupervisable(name));
		_superviser.Supervise(name, new MySupervisable(name));
	}
}
```

{% endcode %}

The test class holds Superviser, MySupervisable is the monitored object in the same test file. The same key is registered repeatedly here to verify that the monitor does not count repeatedly by the number of calls. The test object is not the device protocol client, and network reconnection behavior cannot be inferred based on this.

## Cancel Monitoring and Object Notification

Source: [framework/Zongsoft.Core/test/Components/SuperviserTest.cs](https://github.com/Zongsoft/framework/blob/main/Zongsoft.Core/test/Components/SuperviserTest.cs#L43) (excerpt; see source for context).

{% code title="SuperviserTest.cs" %}

```csharp
public void TestUnsupervise()
{
	this.Initialize(2);

	Assert.True(_superviser.Unsupervise("S1", out var observable));
	Assert.NotNull(observable);
	Assert.IsType<MySupervisable>(observable);
	Assert.Equal("S1", ((MySupervisable)observable).Name);
	Assert.True(((MySupervisable)observable).IsUnsupervised(TimeSpan.FromSeconds(10)));
	Assert.False(_superviser.Contains("S1"));

	Assert.True(_superviser.Unsupervise("S2", out observable));
	Assert.NotNull(observable);
	Assert.IsType<MySupervisable>(observable);
	Assert.Equal("S2", ((MySupervisable)observable).Name);
	Assert.True(((MySupervisable)observable).IsUnsupervised(TimeSpan.FromSeconds(10)));
	Assert.False(_superviser.Contains("S2"));

	Assert.Equal(0, _superviser.Count);
}
```

{% endcode %}

Canceling the operation returns the associated object, after which the key no longer exists. The test waits for the object to receive the cancellation monitoring callback, indicating that the callback and call return times need to be considered separately.

## Concurrent Behavior

The same framework test file also verifies concurrent registration of the same key, concurrent cancellation, and event count. Actual business should specifically check the ownership when repeated registration, cancellation and re-registration are interleaved, and Contains cannot be verified only in a single-threaded environment.

## Lifecycle and Applicable Boundaries

Monitoring lifecycle and failure thresholds describe object monitoring strategies and do not automatically equal network connection survival, process health, or business request success. When implementing monitored objects, it should be clear when data, errors and completions are reported, and how resources are released after monitoring is cancelled.

Monitors themselves should be managed by an explicit service lifecycle; do not create shared monitors for each query, and do not continue to treat the old object as the current connection after cancellation. See [Service ownership](/documentation/en/framework/core/services/locating.md) and [Notifications](/documentation/en/framework/core/common/notification.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.zongsoft.com/documentation/en/framework/core/components/superviser.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
