BackgroundWorker Component For .NET Framework 1.1 with vb.net

Good Morning again!

Well for the last few days, I was asked to work in 1.1 of the Framework with doesn’t have some of our helpful things that we have gotten used to in later versions of the framework. I was asked to write a backgroundworker component developers could use for SQL operations within this .NET Framework 1.1. I have changed the code considerably for security reasons (hope you don’t mind) but for those of you stuck in earlier versions of the framework you might find it useful. It’s now stable enough I think I could share it with you guys. Also, I realize the practice of using TSQL to access the database from code is not a good idea but it is what the boss wanted. So don’t yell at me too loud ok?? :) Hoping my friend Frank Meffert will come along and provide a C# translation of this code. He is quite adept at translating my gibberish. So without further delay here it is. Make it a great day!

Imports System.ComponentModel

Public Class BackgroundWorker

  <EditorBrowsable(EditorBrowsableState.Advanced), Browsable(False)> _
  Delegate Sub BackgroundThreadDelegate()
  <EditorBrowsable(EditorBrowsableState.Advanced), Browsable(False)> _
  Delegate Sub BackgroundThreadDelegateSql(ByVal Sql As String)
  <EditorBrowsable(EditorBrowsableState.Advanced), Browsable(False)> _
  Delegate Sub BackgroundThreadDelegateArgs(ByVal Args() As Object)

  Public Event DoWork(ByVal Sender As Object, ByVal e As DoWorkEventArgs)
  Public Event SqlStatusChanged(ByVal Sender As Object, ByVal e As StatusEventArgs)
  Public Event SqlComplete(ByVal Sender As Object, ByVal e As EventArgs)
  Public Event SqlException(ByVal Sender As Object, ByVal e As SqlExceptionEventArgs)

  <EditorBrowsable(EditorBrowsableState.Advanced), Browsable(False)> _
  Public Enum SqlStatusEnum
    Idle
    Active
  End Enum

  Private _status As SqlStatusEnum

  Public Sub Execute()

    Dim d As BackgroundThreadDelegate = AddressOf BackgroundThread
    d.Invoke()

  End Sub

  Public Sub Execute(ByVal Sql As String)

    Dim d As BackgroundThreadDelegateSql = AddressOf BackgroundThread
    d.Invoke(Sql)

  End Sub

  Public Sub Execute(ByVal args() As Object)

    Dim d As BackgroundThreadDelegateArgs = AddressOf BackgroundThread
    d.Invoke(args)

  End Sub

  Public Sub Wait(ByVal Sql As String)

    Me.Execute(Sql)

    Do
      Application.DoEvents()
    Loop Until Me.SqlStatus = SqlStatusEnum.Idle

  End Sub

  Public Property SqlStatus() As SqlStatusEnum
    Get
      Return _status
    End Get
    Set(ByVal Value As SqlStatusEnum)
      If Value <> _status Then
        _status = Value
        RaiseEvent SqlStatusChanged(Me, New StatusEventArgs(_status))
      End If
    End Set
  End Property

  Private Sub BackgroundThread()
    Threading.Thread.CurrentThread.Sleep(10)
    RaiseEvent DoWork(Me, New DoWorkEventArgs)
  End Sub

  Private Sub BackgroundThread(ByVal Sql As String)

    Dim cn As SqlConnection
    Dim cmd As SqlCommand

    Try

      Me.SqlStatus = SqlStatusEnum.Active

      cn = Context.User.Connection
      cn.Open()
      cmd = New SqlCommand(Sql, cn)
      cmd.CommandTimeout = 600
      cmd.ExecuteNonQuery()
      cn.Close()

      Me.SqlStatus = True
      RaiseEvent SqlComplete(Me, New EventArgs)

    Catch ex As Exception

      If Not cn Is Nothing AndAlso cn.State <> ConnectionState.Closed Then cn.Close()
      RaiseEvent SqlException(Me, New SqlExceptionEventArgs(Sql, ex))
      Me.SqlStatus = SqlStatusEnum.Idle

    End Try

  End Sub

  Private Sub BackgroundThread(ByVal Args() As Object)
    RaiseEvent DoWork(Me, New DoWorkEventArgs(Args))
  End Sub

  <EditorBrowsable(EditorBrowsableState.Advanced), Browsable(False)> _
  Public Class StatusEventArgs
    Inherits EventArgs

    Public sqlStatus As SqlStatusEnum

    Public Sub New(ByVal Status As SqlStatusEnum)
      Me.sqlStatus = Status
    End Sub

  End Class

  <EditorBrowsable(EditorBrowsableState.Advanced), Browsable(False)> _
  Public Class DoWorkEventArgs
    Inherits EventArgs

    Public Sql As String = ""
    Public Args() As Object

    Public Sub New()
    End Sub

    Public Sub New(ByVal Sql As String)
      Me.Sql = Sql
    End Sub

    Public Sub New(ByVal Args() As Object)
      Me.Args = Args
    End Sub

  End Class

  <EditorBrowsable(EditorBrowsableState.Advanced), Browsable(False)> _
  Public Class SqlExceptionEventArgs
    Inherits EventArgs

    Public Exception As Exception
    Public Sql As String

    Public Sub New(ByVal Sql As String, ByVal Exception As Exception)
      Me.Exception = Exception
    End Sub

  End Class

End Class

 

Technorati Tags: ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

Windows Live Tags: vb.net,1.1,.NET Framework,csharp,BackgroundWorker,Component,Framework,TSQL,database,Frank,Meffert,translation,System,ComponentModel,EditorBrowsable,EditorBrowsableState,Browsable,BackgroundThreadDelegate,BackgroundThreadDelegateSql,SqlComplete,SqlException,Execute,BackgroundThread,RaiseEvent,Thread,CurrentThread,SqlConnection,SqlCommand,Context,User,Connection,CommandTimeout,ExecuteNonQuery,ConnectionState,versions,operations

About these ads
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: