Click here to Skip to main content
15,888,113 members
Home / Discussions / C#
   

C#

 
GeneralRe: What is the difference between hashing or encryption a data Pin
Gerry Schmitz31-Mar-17 5:56
mveGerry Schmitz31-Mar-17 5:56 
GeneralRe: What is the difference between hashing or encryption a data Pin
OriginalGriff30-Mar-17 8:52
mveOriginalGriff30-Mar-17 8:52 
GeneralRe: What is the difference between hashing or encryption a data Pin
Gerry Schmitz30-Mar-17 9:04
mveGerry Schmitz30-Mar-17 9:04 
GeneralRe: What is the difference between hashing or encryption a data Pin
Richard Deeming30-Mar-17 9:10
mveRichard Deeming30-Mar-17 9:10 
GeneralRe: What is the difference between hashing or encryption a data Pin
Gerry Schmitz30-Mar-17 9:22
mveGerry Schmitz30-Mar-17 9:22 
GeneralRe: What is the difference between hashing or encryption a data Pin
OriginalGriff30-Mar-17 8:53
mveOriginalGriff30-Mar-17 8:53 
AnswerRe: What is the difference between hashing or encryption a data Pin
Patrice T1-Apr-17 10:34
mvePatrice T1-Apr-17 10:34 
QuestionScheduled Services Architecture - Prototype Pin
Kevin Marois29-Mar-17 12:08
professionalKevin Marois29-Mar-17 12:08 
I was thinking about the design I posted here. So I decided to create a prototype. I welcome your thoughts.

Service Base Class
// Base class for all service classes
   public abstract class ServiceBase
   {
       public enum States
       {
           Cancelled,
           Processing,
           Stopped
       }

       #region Properties
       public States CurrentState { get; protected set; }

       public Guid ServiceId { get; private set; }

       public CancellationTokenSource CancellationTokenSource { get; private set; }
       #endregion

       #region CTOR
       public ServiceBase()
       {
           this.CurrentState = States.Stopped;
           this.ServiceId = Guid.NewGuid();
           this.CancellationTokenSource = new CancellationTokenSource();
       }
       #endregion

       #region Abstract Methods
       public abstract void Start();

       public abstract void Stop();
       #endregion

       #region Public Methods
       public void Cancel()
       {
           this.CurrentState = States.Cancelled;
           CancellationTokenSource.Cancel();
       }
       #endregion

       #region Private Methods
       protected bool CheckIfCancelled(bool throwIfCancelled = false)
       {
           bool cancelled = false;

           var token = CancellationTokenSource.Token;

           if (token.IsCancellationRequested)
           {
               cancelled = true;

               if (throwIfCancelled)
               {
                   token.ThrowIfCancellationRequested();
               }
           }

           return cancelled;
       }
       #endregion
   }
The goal is here to have this base class create and store its own CancellationTokenSource. Then, calling Cancel() on it would allow callers from outside to cancel it. Cancelling is to cancel the Task that it's running it, where Start() and Stop() control the service's procssing.

Service Implementation
Here's an implementation example of that class:
// Service class instance
public class MyService : ServiceBase
{
    #region Public Methods
    public override void Start()
    {
        // If the task has already been cancelled, then throw. This should never happen in my scenerio, but check anyway
        CheckIfCancelled(true);

        // Set the state to processing
        CurrentState = States.Processing;

        // Continue to do work as long as the task is not cancelled
        while (CurrentState == States.Processing)
        {
            if (CheckIfCancelled())
            {
                // Task has been cancelled, so break out of the loop
                break;
            }

            // Other code here
        }
    }

    public override void Stop()
    {
        // Stop processing, but don't cancel.
        CurrentState = States.Stopped;
    }
    #endregion
}
The purpose of this design is to wrap the Cancel code in the base class, so simple implementations like this only need a simple method call to figure out if it's been cancelled.

Usage
The, the Windows Service, which is hosting all the services, looks like this
private static List<ServiceBase> _services;
static void Main(string[] args)
{
    _services = new List<ServiceBase>();

<pre>
//Create a bunch service instances

for (int x = 0; x < 100; x++)
{
MyService service = new MyService(); // See the CTOR of ServiceBase
_services.Add(service);
}
// Start each service
foreach (var service in _services)
{
Task.Factory.StartNew(() =>
{
service.Start();

}, service.CancellationTokenSource.Token);
}
}

Notice that the Task.Factory.StartNew gets the Cancellation Token from the service class.

Stopping or Cancellign
Later on, when it comes time to stop processing, or to Cancel a task, I can do the following:
// LATER ON, WE CAN STOP OR CANCEL A SERVICE
private static ServiceBase GetServiceById(Guid serviceId)
{
    var service = _services.Where(x => x.ServiceId ==1 serviceId).FirstOrDefault();
    return service;
}

var s1 = GetServiceById(_services.FirstOrDefault().ServiceId);
s1.Stop();

var s2 = GetServiceById(_services.Last().ServiceId);
s1.Cancel();
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.

AnswerRe: Scheduled Services Architecture - Prototype Pin
Bernhard Hiller29-Mar-17 21:41
Bernhard Hiller29-Mar-17 21:41 
GeneralRe: Scheduled Services Architecture - Prototype Pin
Kevin Marois30-Mar-17 4:30
professionalKevin Marois30-Mar-17 4:30 
AnswerRe: Scheduled Services Architecture - Prototype Pin
Nathan Minier30-Mar-17 1:30
professionalNathan Minier30-Mar-17 1:30 
AnswerRe: Scheduled Services Architecture - Prototype Pin
Pete O'Hanlon3-Apr-17 2:26
mvePete O'Hanlon3-Apr-17 2:26 
GeneralRe: Scheduled Services Architecture - Prototype Pin
Kevin Marois3-Apr-17 5:38
professionalKevin Marois3-Apr-17 5:38 
QuestionVisual C# Frameworks Pin
zequion28-Mar-17 20:43
professionalzequion28-Mar-17 20:43 
AnswerRe: Visual Studio 2017 Defective Editor in Design Mode Pin
Eddy Vluggen28-Mar-17 22:54
professionalEddy Vluggen28-Mar-17 22:54 
AnswerRe: Visual C# Frameworks Pin
Kevin Marois29-Mar-17 4:18
professionalKevin Marois29-Mar-17 4:18 
QuestionScheduled Services Architecture Pin
Kevin Marois28-Mar-17 7:53
professionalKevin Marois28-Mar-17 7:53 
AnswerRe: Scheduled Services Architecture Pin
Bernhard Hiller28-Mar-17 21:13
Bernhard Hiller28-Mar-17 21:13 
GeneralRe: Scheduled Services Architecture Pin
Kevin Marois29-Mar-17 4:15
professionalKevin Marois29-Mar-17 4:15 
GeneralRe: Scheduled Services Architecture Pin
Kevin Marois29-Mar-17 4:19
professionalKevin Marois29-Mar-17 4:19 
QuestionHandle Exception In Thread Pin
Kevin Marois27-Mar-17 9:35
professionalKevin Marois27-Mar-17 9:35 
AnswerRe: Handle Exception In Thread Pin
Rob Philpott28-Mar-17 0:01
Rob Philpott28-Mar-17 0:01 
GeneralRe: Handle Exception In Thread Pin
Kevin Marois28-Mar-17 5:55
professionalKevin Marois28-Mar-17 5:55 
AnswerRe: Handle Exception In Thread Pin
Nathan Minier28-Mar-17 1:35
professionalNathan Minier28-Mar-17 1:35 
AnswerRe: Handle Exception In Thread Pin
Richard Deeming28-Mar-17 1:43
mveRichard Deeming28-Mar-17 1:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.