Imports DevExpress.XtraSplashScreen
Imports System.Threading
Public Class WaitFormClass
    Private _WaitForm_Minimum As Long = -1
    Private _WaitForm_Maximum As Long = -1
    Private _WaitForm_Step As Long = -1
    Private _Current_Step As Long = -1
    Private _Current_Percent As Double = 0
    Private _Last_Percent As Double = 0
    Private _Act_Displayed_Time As Date
    Private _Prev_Displayed_Time As Date
    Private _StartTime As Date
    Private _Caption As String
    Private _DoThread As Boolean = False
    Private _ThreadHandler As Thread
    Public Property Caption
        Get
            Return _Caption
        End Get
        Set(value)
            _Caption = value
        End Set
    End Property
    Public Sub Initwork(ByVal _Minimum As Long, ByVal _Step As Long, ByVal _Maximum As Long)
        _WaitForm_Minimum = _Minimum
        _WaitForm_Maximum = _Maximum
        _WaitForm_Step = _Step
        _Current_Step = 0
        _Current_Percent = 0
        _Last_Percent = 0
        _Act_Displayed_Time = Now
        _Prev_Displayed_Time = Date.MinValue
        _StartTime = Now
        SplashScreenManager.ShowForm(Nothing, GetType(WaitForm), True, False, False)
        System.Windows.Forms.Application.DoEvents()
        _DoThread = True
        _ThreadHandler = New Thread(AddressOf ThreadTask)
        _ThreadHandler.IsBackground = True
        _ThreadHandler.Start()
    End Sub
    Public Sub DoWork()
        Dim _EstimatedSec As Long = 0
        Dim _SpentSec As Long = 0
        _Current_Step += _WaitForm_Step
        _Current_Percent = Math.Round(_Current_Step / _WaitForm_Maximum * 100, 0, MidpointRounding.AwayFromZero)


        If _Last_Percent <> _Current_Percent Then
            Try
                If Caption = "" Then
                    SplashScreenManager.Default.SetWaitFormCaption(String.Format("Kérem várjon, dolgozom ... {0}", Format(_StartTime, "hh:mm:ss")))
                Else
                    SplashScreenManager.Default.SetWaitFormCaption(Me.Caption)
                End If
                _SpentSec = DateDiff(DateInterval.Second, _StartTime, Now)
                _EstimatedSec = ((_SpentSec / _Current_Step) * (_WaitForm_Maximum - _Current_Step)) + 1
                Dim iSpan As TimeSpan = TimeSpan.FromSeconds(_EstimatedSec)

                SplashScreenManager.Default.SetWaitFormDescription(String.Format("{0}% ,{1}/{2}, eltelt idő: {3}, becsült hátralévő : {4}:{5}:{6}",
                                                                                 _Current_Percent, _Current_Step, _WaitForm_Maximum, FormatEllapsedTime(),
                                                                                 iSpan.Hours.ToString, iSpan.Minutes.ToString, iSpan.Seconds.ToString))
            Catch ex As Exception

            End Try
            _Last_Percent = _Current_Percent
        End If
        System.Windows.Forms.Application.DoEvents()
    End Sub
    Public Sub FinalWork()
        _DoThread = False
        _ThreadHandler = Nothing
        SplashScreenManager.CloseForm(False)
        System.Windows.Forms.Application.DoEvents()
    End Sub
    Private Function FormatEllapsedTime() As String
        Dim iSpan As TimeSpan = TimeSpan.FromSeconds(DateDiff(DateInterval.Second, _StartTime, Now).ToString)
        Return String.Format("{0}:{1}:{2}", iSpan.Hours.ToString, iSpan.Minutes.ToString, iSpan.Seconds.ToString)
    End Function

    '// THREADING
    Private Sub ThreadTask()
        Dim _EstimatedSec As Long = 0
        Dim _SpentSec As Long = 0

        While _DoThread
            _Act_Displayed_Time = Now
            Try
                If _Act_Displayed_Time <> _Prev_Displayed_Time Then
                    If DateDiff(DateInterval.Second, _Prev_Displayed_Time, _Act_Displayed_Time) >= 1 Then
                        _SpentSec = DateDiff(DateInterval.Second, _StartTime, Now)

                        _EstimatedSec = ((_SpentSec / _Current_Step) * (_WaitForm_Maximum - _Current_Step)) + 1
                        Dim iSpan As TimeSpan = TimeSpan.FromSeconds(_EstimatedSec)

                        SplashScreenManager.Default.SetWaitFormDescription(String.Format("{0}% ,{1}/{2}, eltelt idő: {3}, becsült hátralévő : {4}:{5}:{6}",
                        _Current_Percent, _Current_Step, _WaitForm_Maximum, FormatEllapsedTime(),
                        iSpan.Hours.ToString, iSpan.Minutes.ToString, iSpan.Seconds.ToString))

                        _Prev_Displayed_Time = Now
                    End If
                End If
            Catch ex As Exception

            End Try
            Thread.Sleep(1000)
        End While
    End Sub

End Class
