开发者

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 XMLO开发者_运维百科perations();
            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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜