473,804 Members | 3,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem calling SqlConnection.O pen() on a Timer callback

While writing a windows service, I've run into a fairly strange problem. I
have a worker proc that gets fired every n seconds using
System.Threadin g.Timer. This works fine, unless I open an SqlConnection on
it. Just as soon as I do so, the proc fires that time, and never again.
The connection is valid, I can do SQL commands on in with no problems, but
my timer goes away.
Here's a fairly simple console sample that shows the problem. Basically, it
fires up a timer callback every 5 seconds, so you can see it's being called,
then when you hit a keystroke it will call SqlConnect.Open () and
SqlConnect.Clos e() exactly once.

Ideas, anyone?

using System;
using System.Threadin g;
using System.Data.Sql Client;

namespace ThreadPoolTest
{
public class Startup
{
public static int nOpen = 0;

public static void Main()
{
StartupThreads( );

Console.WriteLi ne( "Press Enter to open SQL connection next
time" );
Console.ReadLin e();

Interlocked.Inc rement( ref nOpen );

Console.WriteLi ne( "Press Enter to Exit" );
Console.ReadLin e();
}

public static void StartupThreads( )
{
TimerCallback tc = new TimerCallback( DBFileTimerProc );
Timer t = new Timer( tc, null, 5000, 5000 );
}

static void DBFileTimerProc ( object state )
{
Console.WriteLi ne( "In TimerProc" );
ThreadPool.Queu eUserWorkItem( new WaitCallback( DBFileWorkProc ) );
}

static void DBFileWorkProc( object state )
{
Console.WriteLi ne( "In WorkProc" );
if (nOpen > 0)
{
string strConnect = "Persist Security Info=False;Inte grated
Security=SSPI;d atabase=Northwi nd;server=local host";

using (SqlConnection conn = new SqlConnection( strConnect ))
{
Console.WriteLi ne("Opening connection" );
conn.Open();
conn.Close();
Console.WriteLi ne("connection closed" );
}
}
}
}
}
Nov 16 '05 #1
1 2229
Firstly, thankyou for the fantastic sample that allowed me to work on this.

OK, the problem is in the StartUpThreads method. Here you create the timer, but you assign it to a local variable. The problem is the timer is getting garbage collected. If you move it out to a member variable everything works fine.

So why did it work without opening the connection? because opening the connection causes a whole slew of allocation and this causes a GC, without it a GC just wasn't being triggered.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

While writing a windows service, I've run into a fairly strange problem. I
have a worker proc that gets fired every n seconds using
System.Threadin g.Timer. This works fine, unless I open an SqlConnection on
it. Just as soon as I do so, the proc fires that time, and never again.
The connection is valid, I can do SQL commands on in with no problems, but
my timer goes away.
Here's a fairly simple console sample that shows the problem. Basically, it
fires up a timer callback every 5 seconds, so you can see it's being called,
then when you hit a keystroke it will call SqlConnect.Open () and
SqlConnect.Clos e() exactly once.

Ideas, anyone?
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3945
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
4
2684
by: Todd | last post by:
I have an ASP.NET application and I would like to have some code run on the server automatically once a day at a specified time. I create a timer thread to call a simple callback in the Application_Start method. It seems to call the callback once or twice while the application starts up but appears to stop once the default page is displayed. Here is the code below (from Global.asax.cs): public static void ProcessAccounts (object state)...
3
1389
by: Marten Van Keer | last post by:
Hello; I have an application that waits for TCP packets to arrive on a network stream. A callback function(using an asynccallback delegate) is implemented to tell the application some packets have arrived. At the same time the application uses the Timer object. The Tick-event of this timer is set to 50 seconds. The problem is when the callback function is triggered the timer tick-event
1
2328
by: Wasabi | last post by:
Hi, maybe OT... I'm writing a windowless sw, I need a timer for each object that call a non-static function ( myFunc() ) and refresh a value ( myValue ). I've wrote this code: class myClass {
0
1760
by: Bishoy George | last post by:
Hi, I have a asp.net 2.0 web application. I want to implement the asynchronous model through http handler in web.config ------------------------------------------------------------------------------------------------------------------------- My web.config file: ---------------------
11
1987
by: The Frog | last post by:
Hi all, Maybe I am just missing something simple here, but I seem to have an issue with a callback function in A97 that is used to fill a Listbox with values. The first time the callback function is used (when the form opens) all runs well and everyone is happy. Then comes the problem - values are added to the recordset (ADO) that the callback function uses to populate the listbox. After the new values are added to the recordset, the...
4
1836
by: jwriteclub | last post by:
Hello all, I have a quick question about timers in C#. I have a System.Windows.Forms.Timer timer (the kind you "drag" out of the toolbox. It looks something like this: public class Class {
4
5722
by: =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post by:
Hi, i have a main thread an another worker thread. The main Thread creates another thread and waits for the threads signal to continue the main thread. Everything works inside a ModalDialog and everyting is secured by Invoke/BeginInvoke, and synchronisation primitves like WaitHandles, Evetns, Semaphores, etc... All works good and with no race conditions or locks. I have a System.Windows.Forms.Timer in the main
7
2290
by: genesistr | last post by:
Hi, I have timer class and some classes use timer. but when i come back from timer with callback function, i can see this. properties. function Timer(dName, dCallback){ this.timer = null; this.name = dName; this.isRunning = false; this.interval = 50;
0
9707
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9585
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10586
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.