How to start Timer.Start a service by clicking a button?

Asked

Viewed 23 times

-1

Hello!

Here’s the thing, I have a visual studio solution that has two projects... The first project is a Windows Form system and the second project is a Windows Service that serves as a synchronizer, takes the information that is in the local database and sends it to the web database, and on another machine it takes the information that is in the web database and saved in the local database, this synchronization of the data usually happens every 30 minutes or 1 hour depending on the configuration in the client.

What I’d like to know is this: Is there any way that I can put a button on the Windows Form project that activates the Service’s Timer.Start() function in order for it to synchronize the data the moment that button is pressed? without having to wait the configured time?

I’m not able to find any material to help me make this button that triggers a Windows Service action that is a different project from which the button will be.

If anyone could help me, I’d really appreciate it. Remembering that I use the language VB.NET in Visual Studio 2019.

1 answer

0


If the two applications are on the same machine, you can manipulate them through files.

You make your button create a file inside a folder, which as soon as your other application finds it, it performs the action you want.

For example:

In your windows service project, create a timer with a short interval just by checking the existence of the file.

System.Timers.Timer _timer = new System.Timers.Timer
{
   Interval = 30000,
   Enabled = true,
   AutoReset = true
};
_timer.Elapsed += Init;
_timer.Start();


void Init(object source, ElapsedEventArgs e)
{
    var file = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "my_app_folder", "start_file");
    if (File.Exists(file))
    {
        File.Delete(file); //Deleta para não executar novamente na proxima vez que o timer executar
        //IniciaProcesso
    }
}

It would be interesting also if it were generated a kind of token and written inside the file to ensure that the generated file was really your system.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.