> 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/scheduling.md).

# Zongsoft.Scheduling

Use the framework Hangfire example to concatenate the dispatch contract, handler and host lifecycle.

Scheduling separates "when to execute" from "what to execute". [Core](https://github.com/Zongsoft/framework/tree/main/Zongsoft.Core) provides a public contract for scheduling, and the specific implementation determines delay, cycle, persistence, and failure handling methods. Discussions There is no ready-made scheduling business. This page follows the MyHandler example of the framework.

## Start with Handler

Source: [framework/externals/hangfire/samples/MyHandler.cs](https://github.com/Zongsoft/framework/blob/main/externals/hangfire/samples/MyHandler.cs#L11) (excerpt; see source for context).

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

```csharp
public class MyHandler : HandlerBase<object>
{
	private long _count = 0;

	protected override ValueTask OnHandleAsync(object argument, Parameters parameters, CancellationToken cancellation) =>
		Logging.GetLogging(this).DebugAsync(
			"MyHandler handles the scheduling of the Hangfire.",
			new
			{
				Count = Interlocked.Increment(ref _count),
				Argument = argument,
				Parameters = parameters,
			},
			cancellation);
}
```

{% endcode %}

The handler receives business parameters, extended Parameters and cancellation tokens. The asynchronous interface does not mean that the current work must be time-consuming or require a background thread; the main thing here is log writing.

## Hang to a Location Discoverable by the Scheduler

Source: [framework/externals/hangfire/samples/Zongsoft.Externals.Hangfire.Samples.plugin](https://github.com/Zongsoft/framework/blob/main/externals/hangfire/samples/Zongsoft.Externals.Hangfire.Samples.plugin#L19) (excerpt; see source for context).

{% code title="Zongsoft.Externals.Hangfire.Samples.plugin" %}

```xml
<extension path="/Workbench/Scheduler/Handlers">
	<object name="MyHandler" type="Zongsoft.Externals.Hangfire.Samples.MyHandler, Zongsoft.Externals.Hangfire.Samples" />
</extension>
```

{% endcode %}

Scheduled jobs are associated with handlers by stable names. When renaming types, handler names, or moving plugins, consider references to jobs that already exist in the storage, otherwise the old jobs may still fail to execute after successful deployment.

## Scheduling Methods and Resources

| way                         | Things to check                                                      |
| --------------------------- | -------------------------------------------------------------------- |
| Delayed execution           | Whether the host is running when time base, cancellation, expiration |
| Periodic execution          | cron syntax, time zone, strategy on missed triggers                  |
| persistent job              | Storage availability, restart recovery, parameter compatibility      |
| Multiple instance execution | Allocation mechanism, repetitive execution and idempotent            |

Specific capabilities are subject to implementation. See [Project description](/documentation/en/framework/externals/projects/hangfire.md) for Hangfire integration and storage plugins. The [Core](https://github.com/Zongsoft/framework/tree/main/Zongsoft.Core) contract cannot be understood as automatically having all persistence capabilities.

## Verify and Stop

First build and deploy a real sample, then register MyHandler with the selected scheduler and confirm that the log contains Count, Argument and Parameters. This count is reset to zero on restart and is not shared across multiple workers; it is not a persistent credential for job completion.

Stopping should stop new schedules, handle cancellations, and release service resources. Failed jobs may be retried, and business actions outside the sample require their own idempotent and compensation strategies. For background hosts, see [daemon](/documentation/en/hosting/daemon.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/scheduling.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.
