0

I had created a window services and it will stop when it detect there is changes on my database.

Problem : how to start the window services again after it is stop in maybe 5 or 10 second by coding in C# ?

    private static string connectString = ConfigurationManager.ConnectionStrings['ConnStr'].ToString();

    int sql_depend = 0;//stop

    private delegate void GridDelegate(DataTable table);
    private SqlDependency dep;

    public Watcher()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        SqlDependency.Start(connectString);
        sql_depend = 1;
        UpdateGrid();

        string file = @"E:\WatcherLogFile\sds.txt";
        TextWriter writer = new StreamWriter(file, true);
        writer.WriteLine("Window services started");
        writer.Close();
    }

    protected override void OnStop()
    {
        string file = @"E:\WatcherLogFile\sds.txt";
        TextWriter writer = new StreamWriter(file, true);
        writer.WriteLine("Window services stopped");
        writer.Close();

    }

    //sql dependency check
    private void UpdateGrid()
    {
        string sql = "select [Name], [ClientName] from [Account]";
        DataTable dt = new DataTable();
        using (SqlConnection cn = new SqlConnection(connectString))
        {
            using (SqlCommand cmd = new SqlCommand(sql, cn))
            {
                cn.Open();
                dep = new SqlDependency(cmd);
                dep.OnChange += new OnChangeEventHandler(dep_OnChange);
                using (SqlDataReader rdr = cmd.ExecuteReader())
                {
                    dt.Load(rdr);
                }
            }
        }
    }

    //sql dependency detect changes 
    void dep_OnChange(object sender, SqlNotificationEventArgs e)
    {
        ServiceController myService = new ServiceController();
        myService.ServiceName = "Watcher";
        myService.Stop();
        SqlDependency.Stop(connectString);
        sql_depend = 0;
    }
  • can it be loop function in onStop() ? I want it manually start it once on everyday then it start / stop by itself after that.
tester lol
  • 43
  • 2
  • 8
  • Why do you stop it? Why not simply make it pause whatever it is doing instead? You can't restart a server from within itself, when it has stopped, no code is executing. – Lasse V. Karlsen Jun 01 '15 at 06:12
  • i am new to it, maybe my logic was wrong, hmm, how to pause it and start again? can it be loop ? – tester lol Jun 01 '15 at 08:08
  • Well, the question is, what are you waiting for? Why are you waiting? – Lasse V. Karlsen Jun 01 '15 at 08:09
  • my window services is use to detect any changes on one of my table in the database. Once it detected changes, it stop, then alert me, then i need it start again to run the process again. – tester lol Jun 01 '15 at 08:21
  • How does it alert you? Again, what is it waiting for? Is it waiting for you to acknowledge the alert? I get that *right now* it "waits" for you to start the service. What do you *want* it to wait for? You can describe the process flow you *want* and not the one you *have*? – Lasse V. Karlsen Jun 01 '15 at 08:22
  • okay, now my full process flow is like "watching" a folder-(file watcher) to see is there any text file added into the folder, then I have to convert the txt file and save it into my database(file table). before this step, i had run the sql dependency to see any changes on my table(account table), once it detect changes, i had to stop it , and start my sql dependency again and start the window services again over and over again – tester lol Jun 01 '15 at 08:30
  • OK, Have you considered just using a loop inside the service, and add a step to wait 5 or 10 seconds? – Lasse V. Karlsen Jun 01 '15 at 08:31
  • ya, I had, but i got no idea on how to create it, can you help ? :) – tester lol Jun 01 '15 at 08:34
  • See here: http://stackoverflow.com/questions/2032808/how-to-have-a-loop-in-a-windows-service-without-using-the-timer – Lasse V. Karlsen Jun 01 '15 at 08:39

2 Answers2

0

You can try this.

var sc = new ServiceController(YouServiceNameString);
// stop service
// sc.Stop();

//start service
sc.Start();

// or restart
/*
if (sc.Status.Equals(ServiceControllerStatus.Running))
{
    sc.Stop();
    sc.WaitForStatus(ServiceControllerStatus.Stopped);
 }
 sc.Start();
 sc.WaitForStatus(ServiceControllerStatus.Running);
 */

If you want pause, use Thread.Sleep or this example

var _stopToken = new CancellationTokenSource();
// call this when you want stop
// _stopToken.Cancel();

// there is you can set timeout
_stopToken.Token.WaitHandle.WaitOne(YourWaitTime);

// if you want circle
// while (!_stopToken.IsCancellationRequested)
// { /*...do somthing...*/ }
0

You have various options to solve your problem.

Easiest solution would be configuring recovery action for a service failure.

Check this link for detailed instructions.

Check here

Another option I can think of writing your custom script/program to restart it when you notice the failure.

Hari Prasad
  • 16,716
  • 4
  • 21
  • 35