Book Image

Reactive Programming for .NET Developers

Book Image

Reactive Programming for .NET Developers

Overview of this book

Reactive programming is an innovative programming paradigm focused on time-based problem solving. It makes your programs better-performing, easier to scale, and more reliable. Want to create fast-running applications to handle complex logics and huge datasets for financial and big-data challenges? Then you have picked up the right book! Starting with the principles of reactive programming and unveiling the power of the pull-programming world, this book is your one-stop solution to get a deep practical understanding of reactive programming techniques. You will gradually learn all about reactive extensions, programming, testing, and debugging observable sequence, and integrating events from CLR data-at-rest or events. Finally, you will dive into advanced techniques such as manipulating time in data-flow, customizing operators and providers, and exploring functional reactive programming. By the end of the book, you'll know how to apply reactive programming to solve complex problems and build efficient programs with reactive user interfaces.
Table of Contents (15 chapters)
Reactive Programming for .NET Developers
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface

Designing a custom scheduler


Usually, all the needed schedulers already exists. Although, sometimes, it may happen that we may need a custom scheduler because we may need to choose how to schedule our jobs by ourselves. In the next example, we will see how to create a scheduler to throttle message flowing based on the CPU time. The CpuThrottlingScheduler method will verify each message flowing if the CPU time is at the desired level. Then, eventually, the observer's implementation will receive the message in the context thread of the ThreadPool thread to achieve multithreading if multiple subscribers exist.

Here is the scheduler code:

/// <summary> 
/// Enqueues unit of works only if the current CPU time is lower than the 
/// specified limit. 
/// </summary> 
public class CpuThrottlingScheduler : IScheduler, IDisposable 
{ 
    public int CpuLimitPercentage { get; set; } = 80; 
    public DateTimeOffset Now { get; private set; } 
 
...