【问题标题】:record timestamps in vb.net wpf project from different windows从不同窗口记录 vb.net wpf 项目中的时间戳
【发布时间】:2013-05-12 03:07:15
【问题描述】:

我正在构建一个 vb.net/wpf 应用程序,它是一系列 wpf 窗口。我想在每次打开或关闭新窗口或用户按下按钮时记录时间戳。我正在尝试找到一种解决方案,如何记录事件相对于程序开始发生的时间。我不确定如何创建某种“全局”秒表并从不同的窗口访问它以要求记录时间戳。应该是什么方法?

【问题讨论】:

  • 用静态变量创建一个类 ..

标签: .net wpf vb.net


【解决方案1】:

制作一个负责打开窗户的对象。例如,这可能是 Program 类或 Wizard 类(这对我来说听起来像是一种向导式的 UI)

何时让向导类打开每个窗口并通过调用 ShowDialog 等待它关闭。

如果你这样做了,为窗户的打开和关闭加上时间戳并不难。

当按钮被点击时记录时间戳可以通过让一个对象负责记录并将该对象传递给需要记录的函数来完成。

这很容易污染方法的签名。

有几种方法可以解决这个问题,但大多数方法会创建一个不需要传递的单个众所周知的对象(静态类)。

使用能够根据所需接口解决对象请求的容器可能会更好。 Castle windsor might be an option

//application starts...
var container = new WindsorContainer();

// adds and configures all components using WindsorInstallers from executing assembly
container.Install(FromAssembly.This());

// instantiate and configure root component and all its dependencies and their dependencies and...
var logger= container.Resolve<ILog>();
logger.Log("Click!");

// clean up, application exits
container.Dispose();

【讨论】:

  • @Tim - 是但并非不可能:创建一个对象来记录时间戳,并在需要的地方传递对该对象的引用。
【解决方案2】:

我最终做了以下事情。它包括静态类,我将一个对象(字典)从一个窗口传递到另一个窗口。我不确定这是否是正确的方法,但窗口相互激活并在下一个窗口被称为 showdialog 后关闭。 感谢您的帮助。

Public Class HelpTools
Shared startKey As String = "start_time"
Public Shared Sub initializeData(ByRef data As Dictionary(Of String, String), ByVal id As String, ByVal startTime As DateTime, ByVal firstTimeStamp As Double)

    AddStringOutput(data, "id", id)

    Dim start As String = startTime
    AddStringOutput(data, startKey, start)

    AddStringOutput(data, "next_timestamp", firstTimeStamp)



End Sub

Public Shared Sub recordTime(ByRef data As Dictionary(Of String, String), ByVal info As String)


    'Set the StartTime at the begin of the processing
    ' for which you want to capture ElapsedTime
    Dim StartTime As DateTime = DateTime.Parse(getStringOutput(data, startKey)) 'Now


    'Capture the Elapsed Time here as follows
    Dim ElapsedTime As TimeSpan = Now().Subtract(StartTime)
    'Now we will report the output
    'display format is Hours:Minutes:Seconds
    Dim timestamp As String = String.Format(info & ": elapsed Time : {0:00}:{1:00}:{2:00}", CInt(ElapsedTime.TotalHours), _
    CInt(ElapsedTime.TotalMinutes) Mod 60, _
    CInt(ElapsedTime.TotalSeconds) Mod 60)

    Dim nextTimestampNumber As Integer = getDoubleOutput(data, "next_timestamp")
    AddStringOutput(data, "timestamp_" & nextTimestampNumber, timestamp)
    AddDoubleOutput(data, "next_timestamp", 1)

End Sub

...
end class

【讨论】:

    猜你喜欢
    • 2022-12-08
    • 1970-01-01
    • 2017-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 2023-03-29
    • 2011-03-06
    相关资源
    最近更新 更多