简体   繁体   中英

Event Handler Problem in Windows Service

I am going nuts. I can't figure out the problem.

I have a windows service that has a simple timer method. If I start the service, it always gives out exception at onTimerElapsed event. But If I write my XMLOperation methods in a different method(but not timer which I only need) and call it from program.cs, it works just fine. The working code is at the bottom also.

partial class DatabaseService : ServiceBase
{
    Timer timer = new Timer();

    public DatabaseService()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {   
        timer.Interval = 10000;
        timer.Enabled = true;

        timer.Elapsed += new ElapsedEventHandler(onElapsedTime);
        timer.Start();
    }

    protected override void OnStop()
    {
        timer.Enabled = false;
    }

    public void onElapsedTime(object source, ElapsedEventArgs e)
    {
        try
        {
            XMLOperations operation = new XMLOperations();
            operation.WebServiceFlexiCampaigns("http://www.flexi.com.tr/data/xml/pazaryeri/mobil.xml");
            operation.WebServiceShopMilesCampaignsXMLRead("http://www.shopandmiles.com/xml/3_119_3.xml");
            operation.WebServiceBonusCampaignsXMLRead("http://www.bonus.com.tr/apps/getcampaignxml.aspx?type=campaigns");
        }

        catch (Exception ex)
        {
            StreamWriter SW;
            SW = File.CreateText("c:\\1.txt");
            SW.WriteLine(ex.Message);
            SW.Close();
        }

    }

here is the working one, but this time I could not manage to work that code in periods of time like I can do in timer event. I call test method manually from program.cs

partial class DatabaseService : ServiceBase
{
    Timer timer = new Timer();

    public DatabaseService()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        timer.Interval = 10000;
        timer.Enabled = true;

        timer.Elapsed += new ElapsedEventHandler(onElapsedTime);
        timer.Start();
    }

    protected override void OnStop()
    {
        timer.Enabled = false;
    }
    public void test()
    {
        try
        {
            XMLOperations operation = new XMLOperations();
            operation.WebServiceFlexiCampaigns("http://www.flexi.com.tr/data/xml/pazaryeri/mobil.xml");
            operation.WebServiceShopMilesCampaignsXMLRead("http://www.shopandmiles.com/xml/3_119_3.xml");
            operation.WebServiceBonusCampaignsXMLRead("http://www.bonus.com.tr/apps/getcampaignxml.aspx?type=campaigns");
        }
        catch (Exception ex)
        {
            StreamWriter SW;
            SW = File.CreateText("c:\\1111.txt");
            SW.WriteLine(ex.Message);
            SW.Close();
        }
    }

You can try this thread (see SamAgain response):

http://social.msdn.microsoft.com/Forums/en/clr/thread/8fbca78b-5078-4a12-8abb-4051076febbb

Hope it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM