来自WEB SERVICE的C#窗口服务数据插入

时间:2016-08-25 16:00:06

标签: c# wcf timer windows-services bulkinsert

我有一个从WCF获取数据的Windows服务,它运行正常。我想在5分钟后触发服务。如果有10,000条记录必须插入 - 我想避免插入重复项。如果原始插入未完成,如何增加计时器。

以下是我的代码:

protected override void OnStart(string[] args)
{
    // System.Diagnostics.Debugger.Launch();
    try
    {
        m_mainTimer = new System.Timers.Timer();
        m_mainTimer.Interval = 10*60000;   // every ten min
        m_mainTimer.Elapsed += new System.Timers.ElapsedEventHandler(main_function);
        m_mainTimer.AutoReset = false;  // makes it fire only once
        m_mainTimer.Enabled = true;
    }
    catch (Exception e)
    {
    }           
}

public void main_function(object sender,ElapsedEventArgs e)
{
    while (true)
    {
        try
        {
            FetchRecord fn = new FetchRecord();
            fn.loadNotificationsInEcasDBFromSAP_CTRL();
        }
        catch (Exception ex)
        {           
        }
        finally
        {
            if (null != m_mainTimer)
            {
                m_mainTimer.Start(); // re - enable the timer
            }
        }
    }         
}    

protected override void OnStop()
{
    try
    {
        // Service stopped. Also stop the timer.
        m_mainTimer.Enabled = false;
        m_mainTimer = null;
    }
    catch (Exception ex)
    {
    }
}

0 个答案:

没有答案