Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.ExpressApp.Demos
Public Class LongOperationController
    Inherits DevExpress.ExpressApp.ViewController
    Private progressControl As IProgressControl
    Private waitLongOperationCompleted As AsyncOperation
    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)

    End Sub
    MustOverride Sub DoWorkCore(longOperation As LongOperation)
    MustOverride Function CreateProgressControl() As IProgressControl
    Private Sub DoWork(longOperation As LongOperation)
        Try
            DoWorkCore(longOperation)
        Catch ex As Exception
            longOperation.TerminateAsync()
        End Try
    End Sub
    Private Sub WorkCompleted(state As Object)
        OnOperationCompleted()
    End Sub
    Private Sub LongOperation_CancellingTimeoutExpired(sender As Object, e As EventArgs)
        TryCast(sender, LongOperation).TerminateAsync()
    End Sub
    Private Sub LongOperation_Completed(sender As Object, e As LongOperationCompletedEventArgs)
        progressControl.Dispose()
        progressControl = Nothing
        AddHandler TryCast(sender, LongOperation).CancellingTimeoutExpired, AddressOf LongOperation_CancellingTimeoutExpired
        AddHandler TryCast(sender, LongOperation).Completed, AddressOf LongOperation_Completed
        waitLongOperationCompleted.PostOperationCompleted(AddressOf WorkCompleted, Nothing)
        waitLongOperationCompleted = Nothing
    End Sub
    Protected Sub OnOperationStarted()
        RaiseEvent OperationStarted(Me, Nothing)
    End Sub
    Protected Sub OnOperationCompleted()
        View.ObjectSpace.Refresh()
        RaiseEvent OperationCompleted(Me, Nothing)
    End Sub
    Protected Sub StartLongOperation()
        waitLongOperationCompleted = AsyncOperationManager.CreateOperation(Nothing)
        Dim _longOperation As LongOperation = New LongOperation(AddressOf DoWork)
        _longOperation.CancellingTimeoutMilliSeconds = 10000
        AddHandler _longOperation.CancellingTimeoutExpired, AddressOf LongOperation_CancellingTimeoutExpired
        AddHandler _longOperation.Completed, AddressOf LongOperation_Completed

        progressControl = CreateProgressControl()
        progressControl.ShowProgress(_longOperation)
        _longOperation.StartAsync()
        OnOperationStarted()
    End Sub
    Public Event OperationCompleted As EventHandler
    Public Event OperationStarted As EventHandler
End Class
Public Class LongOperationTerminateException
    Inherits Exception
End Class
<AttributeUsage(AttributeTargets.Class, AllowMultiple:=False, Inherited:=True)> _
Public Class BatchCreationOptionsAttribute
    Inherits Attribute
    Private _objectsCount As Long
    Private _commitInterval As Long
    Sub New()
        MyBase.New()
    End Sub
    Public Sub BatchCreationOptionsAttribute(objectsCount As Long)
        _objectsCount = objectsCount
    End Sub
    Public Sub BatchCreationOptionsAttribute(objectsCount As Long, commitInterval As Integer)
        _objectsCount = objectsCount
        _commitInterval = commitInterval
    End Sub
    ReadOnly Property ObjectsCount As Long
        Get
            Return _objectsCount
        End Get
    End Property
    ReadOnly Property CommitInterval As Long
        Get
            Return _commitInterval
        End Get
    End Property
End Class
Public Interface IObjectPropertiesInitializer
    Sub InitializeObject(index As Integer)
End Interface
Public Interface IProgressControl
    Inherits IDisposable
    Sub ShowProgress(longOperation As LongOperation)
End Interface