开发者

Threading.Timer application is consuming more than 50% of CPU, why?

I have written below console application in VB.net.

My intention is to write an application that triggers every one minute and perform some task. But

when I run this application it is consuming 50% of CPU.

How can I make it to consume less CPU?

Am I calling the timer in the right place (In the main method)?

Later I would like to make a windows service with this same tas开发者_Python百科k and install on the server.

How can I make the application consume less CPU?

Module Module1

    Dim inputPath As String = "C:\Input"
    Dim outputPath As String = "C:\Output"
    Dim folder As Directory

    Sub Main()
        Dim tmr As Timer = New Timer(New TimerCallback(AddressOf Upload), Nothing, 1000, 60000)
        While Not tmr Is Nothing
        End While

    End Sub

    Public Sub Upload(ByVal o As Object)
        Dim sr As StreamReader
        Dim conStr1 As String = "Data Source=TNS Name;User ID=xx; Password=xx;"
        'Look up for pending requests in RQST_TBL
        Dim cnn1 As New OracleConnection(conStr1)
        Dim datReader As OracleDataReader
        Dim cmd1 As New OracleCommand
        cnn1.Open()
        .....
        .....
    End Sub

End Module

Thank you..


I'm guessing you have two CPUs? The infinite while loop is consuming 100% of one CPU; leaving you with 50% total CPU consumption.

From the look of your code - the loop is completely unneeded. Your timer class is going to call the Upload() method when it is complete.

Remove the while loop...

While Not tmr Is Nothing 
End While

And use something like Console.Readline to keep the application from closing.

Alternatively stick a thread.sleep() call inside the while loop if you really like the loop.


While Not tmr Is Nothing
End While

You were already warned about this in a previous question. Delete that code.


While Not tmr Is Nothing
End While

This is just an infinite loop. You're not actually allowing anything to get done.

As this is a console application, you probably only need a loop that sleeps for a minute, then performs your task.


As Rob said, the 50% load probably means you're using 100% of one of your CPUs cores.

Instead of the infinite loop, you can use Console.ReadLine() to keep the console application running.

While the console is waiting for input, your timer will still work as you intend it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜