0

I use Visual Studio 2017. I work with Windows Form Application, Visual C#. There is a timer with 30 seconds interval in my program. Timer starts at Form_Load event method. Actually i use form load method with some other buttons, new settings have been made, changed user etc. Program writes to a text file some data in timer tick event. I test my application with its release built exe. At the beginning, program writes the text file once at 30 seconds. But it does it 4 times after program run a while, maybe 2 hours later.

Example code:

private void timer1_Tick(object sender, EventArgs e)
{
    System.IO.File.AppendAllText(somefilepath, somestringdata + "\r\n");
}

Actually, there is some other processes has to be done at timer event. But they are not about appending the text file. Only the code above is. So can you help me guys? My thought about why it happens, is the setting "Use Parallel Builds". At "Tools -> Options -> Projects and Solutions -> Build and Run" there is an option, "maximum number of parallel project builds". And it is "4" on my settings. Maybe i should do something to make my program run proper with this setting set to 4.

Here's image:

VS Options

How can i make it work proper? Write to text file just once at 30 seconds as i want and as i coded like.

Edit: I closed and opened program, it writes to textfile just once at 30 seconds. It works how it should, so it is not about the size of textfile. Maybe after a while computer creates another instance of program. Is it possible? If yes, can i make it right with Mutex?

Bee-
  • 64
  • 6
  • 2
    there is no relation between parallel build settings and running this method. – DeshDeep Singh Jul 13 '18 at 11:32
  • 1
    Personally, I would stop the timer right at the beginning in the `Tick` event handler and start it again at the end of the very same `Tick` handler. – Uwe Keim Jul 13 '18 at 11:33
  • 1
    Why not show some more code? Like where the timer is instantiated and initialized. – blins Jul 13 '18 at 11:33
  • After some times (2 hr) file size is big. So rewrite the same file it will take some time, next 30 seconds file read/write process won't finishes. So, it repeats same action multiple (4) times. – Niteen Jul 13 '18 at 11:36
  • Timer starts at method of form load event. Actually i call this method with some other buttons, if user changes or some settings have been changed etc. I will try Uwe Keim's suggestion. – Bee- Jul 13 '18 at 11:37
  • @Niteen i think timer stops while timer event method is applying, i mean doesnt it has to wait for my processes to be done first before exiting timer method and start timer again? Maybe i'm wrong, i'm just a beginner. I just think. – Bee- Jul 13 '18 at 11:39
  • @Bee- yupp you were right. Must to stop timer end of the timer1_Tick() method. – Niteen Jul 13 '18 at 11:49
  • 1
    Show the code where you create the timer and add the timer_Tick() eventhandler. I suppose this part is called multiple times, thus the eventhandler is attached multiple times. – derpirscher Jul 13 '18 at 11:50
  • @derpirscher i simply put timer object from windows form controls. Didn't create it with code. That part isn't called multiple times but, the method that starts timer is called multiple times. But i don't think it creates a new timer and runs it. Because it is like that: timer1.Start(); and if it starts timer1, it should restart and after specified timer interval tick event should occur. I will try suggestions now, and write here the responses. – Bee- Jul 13 '18 at 12:07

0 Answers0