Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am writing mail sending script. In main thread the data is brought from database and mail is sent and as the mail sending starts few changes are made in database like sending_status become 'Sending' etc.

I have used timer thread where every 5 mins a function is called and database is updated.

My doubt is that when the main thread is running it will open connection and if the timer thread starts execution it will produce an error because the connection is already opened by main thread and timer thread will try to open connection with database.

What can be solution to this?
Posted
Updated 27-Apr-14 19:19pm
v2
Comments
Sergey Alexandrovich Kryukov 9-Apr-14 1:01am    
Write more reasonable code. You can use a thread instead of timers. Anyway, without seeing your code, it's hard to see where did you screw up.
—SA
Ami_Modi 9-Apr-14 1:06am    
I cant write my script here..

I'd suggest you to use SQLDependency class[^] instead updating database every 5 minutes in thread.


Here is an example: Using SqlDependency for data change events[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 9-Apr-14 2:49am    
Agree. This is more radical approach. My 5.
—SA
Maciej Los 9-Apr-14 2:50am    
Thank you, Sergey ;)
I suggested the polling schema which eliminates a whole bunch of similar problems. Please see: Polling Database with Timer[^].

—SA
 
Share this answer
 
Comments
Maciej Los 9-Apr-14 1:51am    
Self-reference, i like it!
Sergey Alexandrovich Kryukov 9-Apr-14 2:25am    
Thank you, Maciej.
This is a good schema, it solves a number of problem easily.
—SA
Maciej Los 9-Apr-14 2:38am    
Yeah!
Sergey, i think there is another solution for that kind of situation. Please, see my answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900