如何在x时间(分钟和小时)运行任务

时间:2017-09-01 10:37:24

标签: .net vb.net

尝试编写一个需要在x分钟或x小时运行任务的小应用程序。 我面临的是计算x时间以便在指定的分钟或小时运行任务的方法。

我的应用程序非常简单:一个分钟框(分钟(1到59))和一个文本框,用于输入要在指定时间显示的消息。 所以当用户选择50分钟,并输入“Hello”,程序50分钟后,它必须显示一个带有“Hello”消息的消息框,并且应该有多线程以避免在运行一个任务时阻止Ui,另一个也应该是运行

我尝试使用For ... Loop来计算每一秒,但是我没有得到预期的结果。

Dim s as string

For Each s in myVariable
    console.writeLine("Message")
Next

2 个答案:

答案 0 :(得分:0)

您可以使用MediaCreator creator = new MediaCreator(); fileLocation = C:\Images\temp.png var options = new MediaCreatorOptions(); options.Destination = "/sitecore/media library/XYZ/temp"; Item newItem = creator.CreateFromFile(fileLocation, options); 来实现您的目标



Thread.Sleep(milliseconds)



 必须在新的Thread

中完成

参考https://www.dotnetperls.com/sleep-vbnet

或者您可以使用Dim s as string = "Hi" Thread.Sleep(1000) console.writeLine("Message")

Timer

来自https://www.dotnetperls.com/timer-vbnet

的示例

每按一下按钮就会创建一个计时器,您已完成:)

答案 1 :(得分:0)

如果程序在选定的分钟数后显示消息,则可以使用异步方法。

' When combobox value changed
Private Async Sub ComboBox_SelectedIndexChanged(sender As Object, e As EventArgs)
    Dim comboBox = DirectCase(sender, ComboBox)
    Dim minutesAmount = DirectCast(comboBox.SelectedItem, Integer)

    await Task.Delay(minutesAmount * 60000) 

    MessageBox.Show("Hello")
End Sub

Task.Delay将等到给定的毫秒数并继续执行到下一行 await Task.Delay不会阻止UI线程