Solved

System.Timers does not work

Posted on 2006-10-29
2
400 Views
Last Modified: 2008-01-09
Hi,

I have been struggling with this problem for a while and would appreciated any help.

I am trying to use a third party class library in my console application to obtain PageRank for a specific url on a timely bases (every 24 hours).    If I call my "getPageRank" method using a while loop and a thread.sleep() everything works fine.  However, when I try to use the System.timers.Timer object, my program does not execute the functions from the 3rd party DLL (in my case,  pr.GetPageRank(path) does not get executed), and so the application does not progress beyond that point.  I eventually end up getting the following error:

----------------------
The CLR has been unable to transition from COM context 0x1a0c38 to COM context 0x1a0da8 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.
--------------------


 My code is as follow:

-----------------
Private pr As New htmlsnap2Net.CHtmlSnap
Private WithEvents MainTimer As New System.Timers.Timer

 Sub Main()

        MainTimer.Interval = 86400000
        MainTimer.Enabled = True
        MainTimer.AutoReset = True

        Console.WriteLine("Press any key to end!")
        Console.ReadLine()

End Sub

Private Sub MainTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles MainTimer.Elapsed
        Console.WriteLine("PageRank: " & ObtainPageRank("my url goes here"))
End Sub

Private Function ObtainPageRank(ByVal path As String) As Integer
        Dim pagerank As Integer = 0
        pagerank = pr.GetPageRank(path)
       
        Return pagerank
End Function
0
Comment
Question by:espadana
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 70 total points
ID: 17829527
Explore using the ApplicationContext() class to keep your app from ending:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.applicationcontext.aspx

Something like...

Module Module1

    Public Sub Main()
        Application.Run(New MyContext)
    End Sub

    Public Class MyContext
        Inherits ApplicationContext

        Private pr As New htmlsnap2Net.CHtmlSnap
        Private WithEvents MainTimer As New System.Timers.Timer

        Public Sub New()
            MainTimer.Interval = TimeSpan.TicksPerDay '86400000
            MainTimer.AutoReset = True
            MainTimer.Start()
        End Sub

        Private Sub MainTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles MainTimer.Elapsed
            Console.WriteLine("PageRank: " & ObtainPageRank("my url goes here"))
        End Sub

        Private Function ObtainPageRank(ByVal path As String) As Integer
            Dim pagerank As Integer = 0
            pagerank = pr.GetPageRank(path)

            Return pagerank
        End Function

    End Class

End Module
0
 

Author Comment

by:espadana
ID: 17832572
Thank you! It seems to be working
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question