I am trying to create a Windows Service that will read each row of a text file and insert them into a SQL database. The text file can have a max of 150,000 rows. When I try to read one line from the text file, insert into the database and reiterate up to the max, the CPU maxes out. This service needs to run in the background using as little processor power as possible. Do you have any ideas on how to accomplish this? Thanks for your time.
The problem is that you're not playing nice with the CPU. When your program goes into that tight loop reading the lines, the thread scheduler sees you're not waiting on anything from the OS and will continually schedule the thread. What I would do is the following:
- Call SetTimer and tell the API to call back one of your functions. I'd set the elapsed time to something like 750 milliseconds or even a second.
- In your timer function, I'd read three to five lines and insert those lines into the database before ending.
By using the timer, you'll break up the processing and thus ensure you're not hogging the CPU.
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.