Convert Simple C# Console>Service to perform repeated API calls and file system actions

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












I was sent here from StackOverflow, as it was indicated that the quality of my current approach may have been questionable. I'm attempting to transform a simple console application so that it may be used as a service using a nested Service class (relevant code below). The intent is to create a service to perform the actions within the Run method more-or-less at a regular interval. I'm new to .NET, so please bare with me if there are obvious flaws in my implementation.



In short, the Service's OnStart calls my own Start method once, which creates a timer. Inside the timer, I'm creating an instance of the Program class, and performing the Run method till it finishes. I believe this is a valid implementation of what I want that doesn't leave resources outstanding, although I am unsure whether I should dispose of the Prg object at the end of CallTimer. Part of what the Run method does is build a dictionary of actions on the file system that were unsuccessful so that they may be attempted again in the next run.



I'm a bit green and not terribly confident that I'm doing things correctly, but I'm attempting to get up to speed as quickly as possible, so any assistance is appreciated. I can provide additional details if needed.



partial class Program
static object timerLck = new object();

public class Service : ServiceBase
public Service()
ServiceName = Program.ServiceName;

protected override void OnStart(string args)
Program.Start(args);

protected override void OnStop()
Program.Stop();



static void Main(string args)
if (!Environment.UserInteractive)
using (var service = new Service())
ServiceBase.Run(service);
else
Start(args);
Stop();



private static void Start(string args)
Timer t = new Timer(CallTimer, null, 0, 50000);
Console.ReadKey();


private static void Stop()
//log service stopped


static void CallTimer(object state)
bool Lock = false;
Dictionary<string, Metadata> PreviousErrors = new Dictionary<string, Metadata>();
try
Lock = Monitor.TryEnter(timerLck);
if (Lock)
Program Prg = new Program()
var task = Task.Run(()=>Prg.Run(PreviousErrors));
PreviousErrors = task.Result;


finally
if (Lock) Monitor.Exit(timerLck);



//Perform the repeated actions here
private async Task<Dictionary<string, Metadata>> Run(Dictionary<string, Metadata> previousErrors)
Dictionary<string, Metadata> d = new Dictionary<string, Metadata>();
//...
return d;








share|improve this question

















  • 2




    This //??? and that //... is off-topic on Code Review. You need to put something in there, this is, post the complete code ;-)
    – t3chb0t
    Feb 8 at 21:19











  • t3chb0t, Thank you for responding and for the edit. I'm not currently doing anything in Stop other than logging that the service stopped (I have now clarified the comment). I left the contents off of Run because it would increase the code substantially and I was really hoping to get clarification on the overall structure of the Service rather than the details of Run, which changed minimally. Is this inappropriate?
    – AliceSmith
    Feb 8 at 21:53







  • 2




    Where is ServiceBase coming from (I'm guessing System.ServiceProcess, but why do I need to guess -- you should include the relevant usings)? There is no ServiceName in Program, so where is that coming from? What is Metadata? Program Prg = new Program() is lacking a ; at the end. Putting { at the end of the line instead of on a line of their own is not a standard C# coding style.
    – BCdotWEB
    Feb 9 at 10:53










  • The character limit for a question on Code Review is over 65k characters. You won't even come close. Please provide the full code, including usings and imports of types.
    – Mast
    Feb 11 at 8:16










  • Please move the Service class and the timer code to a seperate file. Also place the opening brackets on a new line. file. It is cleaner and easier to read.
    – Bob Lokerse
    Mar 15 at 16:55

















up vote
2
down vote

favorite












I was sent here from StackOverflow, as it was indicated that the quality of my current approach may have been questionable. I'm attempting to transform a simple console application so that it may be used as a service using a nested Service class (relevant code below). The intent is to create a service to perform the actions within the Run method more-or-less at a regular interval. I'm new to .NET, so please bare with me if there are obvious flaws in my implementation.



In short, the Service's OnStart calls my own Start method once, which creates a timer. Inside the timer, I'm creating an instance of the Program class, and performing the Run method till it finishes. I believe this is a valid implementation of what I want that doesn't leave resources outstanding, although I am unsure whether I should dispose of the Prg object at the end of CallTimer. Part of what the Run method does is build a dictionary of actions on the file system that were unsuccessful so that they may be attempted again in the next run.



I'm a bit green and not terribly confident that I'm doing things correctly, but I'm attempting to get up to speed as quickly as possible, so any assistance is appreciated. I can provide additional details if needed.



partial class Program
static object timerLck = new object();

public class Service : ServiceBase
public Service()
ServiceName = Program.ServiceName;

protected override void OnStart(string args)
Program.Start(args);

protected override void OnStop()
Program.Stop();



static void Main(string args)
if (!Environment.UserInteractive)
using (var service = new Service())
ServiceBase.Run(service);
else
Start(args);
Stop();



private static void Start(string args)
Timer t = new Timer(CallTimer, null, 0, 50000);
Console.ReadKey();


private static void Stop()
//log service stopped


static void CallTimer(object state)
bool Lock = false;
Dictionary<string, Metadata> PreviousErrors = new Dictionary<string, Metadata>();
try
Lock = Monitor.TryEnter(timerLck);
if (Lock)
Program Prg = new Program()
var task = Task.Run(()=>Prg.Run(PreviousErrors));
PreviousErrors = task.Result;


finally
if (Lock) Monitor.Exit(timerLck);



//Perform the repeated actions here
private async Task<Dictionary<string, Metadata>> Run(Dictionary<string, Metadata> previousErrors)
Dictionary<string, Metadata> d = new Dictionary<string, Metadata>();
//...
return d;








share|improve this question

















  • 2




    This //??? and that //... is off-topic on Code Review. You need to put something in there, this is, post the complete code ;-)
    – t3chb0t
    Feb 8 at 21:19











  • t3chb0t, Thank you for responding and for the edit. I'm not currently doing anything in Stop other than logging that the service stopped (I have now clarified the comment). I left the contents off of Run because it would increase the code substantially and I was really hoping to get clarification on the overall structure of the Service rather than the details of Run, which changed minimally. Is this inappropriate?
    – AliceSmith
    Feb 8 at 21:53







  • 2




    Where is ServiceBase coming from (I'm guessing System.ServiceProcess, but why do I need to guess -- you should include the relevant usings)? There is no ServiceName in Program, so where is that coming from? What is Metadata? Program Prg = new Program() is lacking a ; at the end. Putting { at the end of the line instead of on a line of their own is not a standard C# coding style.
    – BCdotWEB
    Feb 9 at 10:53










  • The character limit for a question on Code Review is over 65k characters. You won't even come close. Please provide the full code, including usings and imports of types.
    – Mast
    Feb 11 at 8:16










  • Please move the Service class and the timer code to a seperate file. Also place the opening brackets on a new line. file. It is cleaner and easier to read.
    – Bob Lokerse
    Mar 15 at 16:55













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I was sent here from StackOverflow, as it was indicated that the quality of my current approach may have been questionable. I'm attempting to transform a simple console application so that it may be used as a service using a nested Service class (relevant code below). The intent is to create a service to perform the actions within the Run method more-or-less at a regular interval. I'm new to .NET, so please bare with me if there are obvious flaws in my implementation.



In short, the Service's OnStart calls my own Start method once, which creates a timer. Inside the timer, I'm creating an instance of the Program class, and performing the Run method till it finishes. I believe this is a valid implementation of what I want that doesn't leave resources outstanding, although I am unsure whether I should dispose of the Prg object at the end of CallTimer. Part of what the Run method does is build a dictionary of actions on the file system that were unsuccessful so that they may be attempted again in the next run.



I'm a bit green and not terribly confident that I'm doing things correctly, but I'm attempting to get up to speed as quickly as possible, so any assistance is appreciated. I can provide additional details if needed.



partial class Program
static object timerLck = new object();

public class Service : ServiceBase
public Service()
ServiceName = Program.ServiceName;

protected override void OnStart(string args)
Program.Start(args);

protected override void OnStop()
Program.Stop();



static void Main(string args)
if (!Environment.UserInteractive)
using (var service = new Service())
ServiceBase.Run(service);
else
Start(args);
Stop();



private static void Start(string args)
Timer t = new Timer(CallTimer, null, 0, 50000);
Console.ReadKey();


private static void Stop()
//log service stopped


static void CallTimer(object state)
bool Lock = false;
Dictionary<string, Metadata> PreviousErrors = new Dictionary<string, Metadata>();
try
Lock = Monitor.TryEnter(timerLck);
if (Lock)
Program Prg = new Program()
var task = Task.Run(()=>Prg.Run(PreviousErrors));
PreviousErrors = task.Result;


finally
if (Lock) Monitor.Exit(timerLck);



//Perform the repeated actions here
private async Task<Dictionary<string, Metadata>> Run(Dictionary<string, Metadata> previousErrors)
Dictionary<string, Metadata> d = new Dictionary<string, Metadata>();
//...
return d;








share|improve this question













I was sent here from StackOverflow, as it was indicated that the quality of my current approach may have been questionable. I'm attempting to transform a simple console application so that it may be used as a service using a nested Service class (relevant code below). The intent is to create a service to perform the actions within the Run method more-or-less at a regular interval. I'm new to .NET, so please bare with me if there are obvious flaws in my implementation.



In short, the Service's OnStart calls my own Start method once, which creates a timer. Inside the timer, I'm creating an instance of the Program class, and performing the Run method till it finishes. I believe this is a valid implementation of what I want that doesn't leave resources outstanding, although I am unsure whether I should dispose of the Prg object at the end of CallTimer. Part of what the Run method does is build a dictionary of actions on the file system that were unsuccessful so that they may be attempted again in the next run.



I'm a bit green and not terribly confident that I'm doing things correctly, but I'm attempting to get up to speed as quickly as possible, so any assistance is appreciated. I can provide additional details if needed.



partial class Program
static object timerLck = new object();

public class Service : ServiceBase
public Service()
ServiceName = Program.ServiceName;

protected override void OnStart(string args)
Program.Start(args);

protected override void OnStop()
Program.Stop();



static void Main(string args)
if (!Environment.UserInteractive)
using (var service = new Service())
ServiceBase.Run(service);
else
Start(args);
Stop();



private static void Start(string args)
Timer t = new Timer(CallTimer, null, 0, 50000);
Console.ReadKey();


private static void Stop()
//log service stopped


static void CallTimer(object state)
bool Lock = false;
Dictionary<string, Metadata> PreviousErrors = new Dictionary<string, Metadata>();
try
Lock = Monitor.TryEnter(timerLck);
if (Lock)
Program Prg = new Program()
var task = Task.Run(()=>Prg.Run(PreviousErrors));
PreviousErrors = task.Result;


finally
if (Lock) Monitor.Exit(timerLck);



//Perform the repeated actions here
private async Task<Dictionary<string, Metadata>> Run(Dictionary<string, Metadata> previousErrors)
Dictionary<string, Metadata> d = new Dictionary<string, Metadata>();
//...
return d;










share|improve this question












share|improve this question




share|improve this question








edited Feb 8 at 21:45
























asked Feb 8 at 21:14









AliceSmith

112




112







  • 2




    This //??? and that //... is off-topic on Code Review. You need to put something in there, this is, post the complete code ;-)
    – t3chb0t
    Feb 8 at 21:19











  • t3chb0t, Thank you for responding and for the edit. I'm not currently doing anything in Stop other than logging that the service stopped (I have now clarified the comment). I left the contents off of Run because it would increase the code substantially and I was really hoping to get clarification on the overall structure of the Service rather than the details of Run, which changed minimally. Is this inappropriate?
    – AliceSmith
    Feb 8 at 21:53







  • 2




    Where is ServiceBase coming from (I'm guessing System.ServiceProcess, but why do I need to guess -- you should include the relevant usings)? There is no ServiceName in Program, so where is that coming from? What is Metadata? Program Prg = new Program() is lacking a ; at the end. Putting { at the end of the line instead of on a line of their own is not a standard C# coding style.
    – BCdotWEB
    Feb 9 at 10:53










  • The character limit for a question on Code Review is over 65k characters. You won't even come close. Please provide the full code, including usings and imports of types.
    – Mast
    Feb 11 at 8:16










  • Please move the Service class and the timer code to a seperate file. Also place the opening brackets on a new line. file. It is cleaner and easier to read.
    – Bob Lokerse
    Mar 15 at 16:55













  • 2




    This //??? and that //... is off-topic on Code Review. You need to put something in there, this is, post the complete code ;-)
    – t3chb0t
    Feb 8 at 21:19











  • t3chb0t, Thank you for responding and for the edit. I'm not currently doing anything in Stop other than logging that the service stopped (I have now clarified the comment). I left the contents off of Run because it would increase the code substantially and I was really hoping to get clarification on the overall structure of the Service rather than the details of Run, which changed minimally. Is this inappropriate?
    – AliceSmith
    Feb 8 at 21:53







  • 2




    Where is ServiceBase coming from (I'm guessing System.ServiceProcess, but why do I need to guess -- you should include the relevant usings)? There is no ServiceName in Program, so where is that coming from? What is Metadata? Program Prg = new Program() is lacking a ; at the end. Putting { at the end of the line instead of on a line of their own is not a standard C# coding style.
    – BCdotWEB
    Feb 9 at 10:53










  • The character limit for a question on Code Review is over 65k characters. You won't even come close. Please provide the full code, including usings and imports of types.
    – Mast
    Feb 11 at 8:16










  • Please move the Service class and the timer code to a seperate file. Also place the opening brackets on a new line. file. It is cleaner and easier to read.
    – Bob Lokerse
    Mar 15 at 16:55








2




2




This //??? and that //... is off-topic on Code Review. You need to put something in there, this is, post the complete code ;-)
– t3chb0t
Feb 8 at 21:19





This //??? and that //... is off-topic on Code Review. You need to put something in there, this is, post the complete code ;-)
– t3chb0t
Feb 8 at 21:19













t3chb0t, Thank you for responding and for the edit. I'm not currently doing anything in Stop other than logging that the service stopped (I have now clarified the comment). I left the contents off of Run because it would increase the code substantially and I was really hoping to get clarification on the overall structure of the Service rather than the details of Run, which changed minimally. Is this inappropriate?
– AliceSmith
Feb 8 at 21:53





t3chb0t, Thank you for responding and for the edit. I'm not currently doing anything in Stop other than logging that the service stopped (I have now clarified the comment). I left the contents off of Run because it would increase the code substantially and I was really hoping to get clarification on the overall structure of the Service rather than the details of Run, which changed minimally. Is this inappropriate?
– AliceSmith
Feb 8 at 21:53





2




2




Where is ServiceBase coming from (I'm guessing System.ServiceProcess, but why do I need to guess -- you should include the relevant usings)? There is no ServiceName in Program, so where is that coming from? What is Metadata? Program Prg = new Program() is lacking a ; at the end. Putting { at the end of the line instead of on a line of their own is not a standard C# coding style.
– BCdotWEB
Feb 9 at 10:53




Where is ServiceBase coming from (I'm guessing System.ServiceProcess, but why do I need to guess -- you should include the relevant usings)? There is no ServiceName in Program, so where is that coming from? What is Metadata? Program Prg = new Program() is lacking a ; at the end. Putting { at the end of the line instead of on a line of their own is not a standard C# coding style.
– BCdotWEB
Feb 9 at 10:53












The character limit for a question on Code Review is over 65k characters. You won't even come close. Please provide the full code, including usings and imports of types.
– Mast
Feb 11 at 8:16




The character limit for a question on Code Review is over 65k characters. You won't even come close. Please provide the full code, including usings and imports of types.
– Mast
Feb 11 at 8:16












Please move the Service class and the timer code to a seperate file. Also place the opening brackets on a new line. file. It is cleaner and easier to read.
– Bob Lokerse
Mar 15 at 16:55





Please move the Service class and the timer code to a seperate file. Also place the opening brackets on a new line. file. It is cleaner and easier to read.
– Bob Lokerse
Mar 15 at 16:55
















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f187126%2fconvert-simple-c-consoleservice-to-perform-repeated-api-calls-and-file-system%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f187126%2fconvert-simple-c-consoleservice-to-perform-repeated-api-calls-and-file-system%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation