Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 线程。定时器应用程序消耗了超过50%的CPU,为什么?_Vb.net_Windows Services_Console Application_Visual Studio 2003_Timertask - Fatal编程技术网

Vb.net 线程。定时器应用程序消耗了超过50%的CPU,为什么?

Vb.net 线程。定时器应用程序消耗了超过50%的CPU,为什么?,vb.net,windows-services,console-application,visual-studio-2003,timertask,Vb.net,Windows Services,Console Application,Visual Studio 2003,Timertask,我在VB.net中编写了以下控制台应用程序 我的意图是编写一个每分钟触发一次的应用程序并执行一些任务。但是 当我运行这个应用程序时,它消耗了50%的CPU 我怎样才能使它消耗更少的CPU Module Module1 Dim inputPath As String = "C:\Input" Dim outputPath As String = "C:\Output" Dim folder As Directory Sub Main() Dim

我在VB.net中编写了以下控制台应用程序

我的意图是编写一个每分钟触发一次的应用程序并执行一些任务。但是

当我运行这个应用程序时,它消耗了50%的CPU

我怎样才能使它消耗更少的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
我是否在正确的位置调用计时器(在主方法中)

稍后,我想使用相同的任务创建一个windows服务,并在服务器上安装

如何使应用程序消耗更少的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
谢谢

While Not tmr Is Nothing
End While
在前面的问题中已经警告过您这一点。删除该代码

While Not tmr Is Nothing
End While
在前面的问题中已经警告过您这一点。删除该代码

While Not tmr Is Nothing
End While
这只是一个无限循环。你实际上不允许做任何事情

由于这是一个控制台应用程序,您可能只需要一个休眠一分钟的循环,然后执行任务

这只是一个无限循环。你实际上不允许做任何事情


由于这是一个控制台应用程序,您可能只需要一个休眠一分钟的循环,然后执行任务。

我猜您有两个CPU?无限while循环消耗一个CPU的100%;留给您50%的CPU总消耗

从代码的外观来看,完全不需要循环。完成后,计时器类将调用Upload()方法

删除while循环

While Not tmr Is Nothing 
End While
并使用类似Console.Readline的东西来防止应用程序关闭


如果您真的喜欢while循环,也可以在while循环中插入thread.sleep()调用。

我猜您有两个CPU?无限while循环消耗一个CPU的100%;留给您50%的CPU总消耗

从代码的外观来看,完全不需要循环。完成后,计时器类将调用Upload()方法

删除while循环

While Not tmr Is Nothing 
End While
并使用类似Console.Readline的东西来防止应用程序关闭


如果您真的喜欢while循环,也可以在while循环中插入thread.sleep()调用。

正如Rob所说,50%的负载可能意味着您使用了100%的CPU内核

您可以使用
Console.ReadLine()
来保持控制台应用程序运行,而不是无限循环


当控制台等待输入时,您的计时器仍将按预期工作。

正如Rob所说,50%的负载可能意味着您正在使用100%的CPU内核

您可以使用
Console.ReadLine()
来保持控制台应用程序运行,而不是无限循环


当控制台等待输入时,计时器仍将按预期工作。

我在While循环中添加了Thread.Sleep(60000),它占用了几乎0%的CPU。非常感谢你。很抱歉,我错过了前面的警告。我在while循环中添加了Thread.Sleep(60000),它占用了几乎0%的CPU。非常感谢你。很抱歉我错过了之前的警告谢谢。我这样做了,它没有给CPU带来负载。谢谢。我这样做了,它没有给CPU带来负载。