Convert Simple C# Console>Service to perform repeated API calls and file system actions
Clash 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;
c# beginner .net
add a comment |Â
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;
c# beginner .net
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 inStop
other than logging that the service stopped (I have now clarified the comment). I left the contents off ofRun
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 ofRun
, which changed minimally. Is this inappropriate?
â AliceSmith
Feb 8 at 21:53
2
Where isServiceBase
coming from (I'm guessingSystem.ServiceProcess
, but why do I need to guess -- you should include the relevantusing
s)? There is noServiceName
inProgram
, so where is that coming from? What isMetadata
?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
add a comment |Â
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;
c# beginner .net
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;
c# beginner .net
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 inStop
other than logging that the service stopped (I have now clarified the comment). I left the contents off ofRun
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 ofRun
, which changed minimally. Is this inappropriate?
â AliceSmith
Feb 8 at 21:53
2
Where isServiceBase
coming from (I'm guessingSystem.ServiceProcess
, but why do I need to guess -- you should include the relevantusing
s)? There is noServiceName
inProgram
, so where is that coming from? What isMetadata
?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
add a comment |Â
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 inStop
other than logging that the service stopped (I have now clarified the comment). I left the contents off ofRun
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 ofRun
, which changed minimally. Is this inappropriate?
â AliceSmith
Feb 8 at 21:53
2
Where isServiceBase
coming from (I'm guessingSystem.ServiceProcess
, but why do I need to guess -- you should include the relevantusing
s)? There is noServiceName
inProgram
, so where is that coming from? What isMetadata
?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 using
s)? 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 using
s)? 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
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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 ofRun
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 ofRun
, which changed minimally. Is this inappropriate?â AliceSmith
Feb 8 at 21:53
2
Where is
ServiceBase
coming from (I'm guessingSystem.ServiceProcess
, but why do I need to guess -- you should include the relevantusing
s)? There is noServiceName
inProgram
, so where is that coming from? What isMetadata
?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