Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3759584
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:20:20+00:00 2026-05-19T10:20:20+00:00

I have a .NET class, InstrumentConnector, that has an event, StatusChanged. I want to

  • 0

I have a .NET class, InstrumentConnector, that has an event, StatusChanged. I want to create several instances of this class (each class handles a separate file) and respond when the StatusChanged event is raised any instance. How would I do this in .NET? Do I want to create a List(Of InstrumentConnector) or a similar IEnumerable? How do I write the event handler to respond to a particular instance’s StatusChanged event?

Here is the InstrumentConnector class and the module with the StatusChanged handler:

Public Class InstrumentConnector

Private _inProcessDir As String
Private _doneDir As String

Private _dataFileName As String
Public ReadOnly Property DataFileName() As String
    Get
        Return _dataFileName
    End Get

End Property


Private WithEvents _processTimer As Timers.Timer

Private _fileStatus As Status
Public ReadOnly Property FileStatus() As Status
    Get
        Return _fileStatus
    End Get

End Property


Public Sub New(ByVal inProcessDirectory As String, ByVal doneDirectory As String)
    _inProcessDir = inProcessDirectory
    _doneDir = doneDirectory
    _fileStatus = Status.Waiting
End Sub

Public Sub New(ByVal inProcessDirectory As String)
    Me.New(inProcessDirectory, IO.Path.Combine(inProcessDirectory, "done"))
End Sub

Private Sub InitializeTimer()
    _processTimer = New Timers.Timer
    With _processTimer
        .Interval = 1000
    End With
End Sub

Public Sub Process(ByVal dataFileName As String)

    _dataFileName = IO.Path.GetFileName(dataFileName)

    InitializeTimer()

    _processTimer.Start()

End Sub

Private Sub _processTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles _processTimer.Elapsed

    '1. Look for file in Done directory
    '2. Evaluate its approval status
    '3. If not completed change approval setting in file and move to InProcess directory

    If IsFileInProcessDirectory() = True Then
        MoveFileToDoneDirectory()
    Else
        If IsFileInDoneDirectory() = True Then
            If IsFileApproved() = True Then
                _fileStatus = Status.Accepted
                RaiseEvent StatusChanged(Nothing)
            Else
                If NeedsSecondAttempt() = True Then
                    MoveFileToProcessDirectory()
                Else
                    _fileStatus = Status.Rejected
                    RaiseEvent StatusChanged(Nothing)
                End If

            End If

        End If
    End If

End Sub

Private Sub MoveFileToDoneDirectory()

    Dim fileContents As String
    fileContents = My.Computer.FileSystem.ReadAllText(IO.Path.Combine(_inProcessDir, _dataFileName))

    If GetFileApprovalStatus() = True Then
        fileContents = String.Concat("9999", fileContents.Substring(4))
    End If

    My.Computer.FileSystem.WriteAllText(IO.Path.Combine(_doneDir, _dataFileName), fileContents, False)
    My.Computer.FileSystem.DeleteFile(IO.Path.Combine(_inProcessDir, _dataFileName))

End Sub

Private Function NeedsSecondAttempt() As Boolean
    Dim fileContents As String
    fileContents = My.Computer.FileSystem.ReadAllText(IO.Path.Combine(_doneDir, _dataFileName))

    If fileContents.StartsWith("9999") And fileContents.Substring(111, 1) = "1" Then
        Return False
    Else
        Return True
    End If

End Function

Private Sub MoveFileToProcessDirectory()

    Dim fileContents As String
    fileContents = My.Computer.FileSystem.ReadAllText(IO.Path.Combine(_doneDir, _dataFileName))

    fileContents = fileContents.Remove(111, 1).Insert(111, "1")
    My.Computer.FileSystem.WriteAllText(IO.Path.Combine(_doneDir, _dataFileName), fileContents, False)

    My.Computer.FileSystem.MoveFile(IO.Path.Combine(_doneDir, _dataFileName), IO.Path.Combine(_inProcessDir, _dataFileName))

End Sub

Private Function IsFileInDoneDirectory() As Boolean

    Dim fileExists As Boolean
    fileExists = My.Computer.FileSystem.FileExists(IO.Path.Combine(_doneDir, _dataFileName))

    Return fileExists

End Function

Private Function IsFileInProcessDirectory() As Boolean

    Dim fileExists As Boolean
    fileExists = My.Computer.FileSystem.FileExists(IO.Path.Combine(_inProcessDir, _dataFileName))

    Return fileExists

End Function

Private Function IsFileApproved() As Boolean

    Dim fileContents As String
    fileContents = My.Computer.FileSystem.ReadAllText(IO.Path.Combine(_doneDir, _dataFileName))

    If fileContents.Substring(0, 4) = "9999" Then
        Return True
    Else
        Return False
    End If

End Function

Private Function GetFileApprovalStatus() As Boolean

    Dim randNum As New Random
    Dim nextNum = randNum.Next(1, 10)

    '60% of all samples will be approved
    If nextNum > 6 Then
        Return False
    Else
        Return True
    End If

End Function

Public Event StatusChanged(ByVal e As System.EventArgs)

Protected Overridable Sub OnStatusChanged(ByVal e As System.EventArgs) Handles Me.StatusChanged
    _processTimer.Stop()
End Sub

Protected Overrides Sub Finalize()
    MyBase.Finalize()
    _processTimer.Stop()

End Sub

Public Enum Status
    Accepted
    Rejected
    Waiting
End Enum

End Class

Module Module1

Private WithEvents detector As NewFileDetector
Private WithEvents newIC As InstrumentConnector
Private ICList As New List(Of InstrumentConnector)
Private fileDirectory As String

Sub Main()

    fileDirectory = "c:\temp\inproc"

    Console.WriteLine("Mock IC program running...")
    Console.WriteLine()
    Console.WriteLine("Press ESC to quit...")

    detector = New NewFileDetector(fileDirectory)


    Dim keyresponse As ConsoleKeyInfo = Nothing

    Do
        keyresponse = Console.ReadKey

    Loop Until keyresponse.Key = ConsoleKey.Escape 'Quit program on ESC

End Sub

Private Sub detector_FilesFound(ByVal e As System.EventArgs) Handles detector.FilesFound

    Console.WriteLine("Files found: {0}", detector.FileList.Count)

    For Each filename In detector.FileList
        newIC = New InstrumentConnector(fileDirectory)
        ICList.Add(newIC)
        newIC.Process(filename)
    Next

End Sub

Private Sub newIC_StatusChanged(ByVal e As System.EventArgs) Handles newIC.StatusChanged
    Console.WriteLine("File: {0} Status: {1}", newIC.DataFileName, newIC.FileStatus.ToString)
    ICList.Remove(newIC)

End Sub

End Module

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T10:20:21+00:00Added an answer on May 19, 2026 at 10:20 am

    How you store the InstrumentConnectors is not related t the events. A List(Of T) is fine.

    And you can hook up a listener class to multiple sources of events. That is why the standard pattern in .NET has the sender parameter. Just cast that to an InstrumentConnector and you’re done.

    That is, as long as StatusChanged is designed according to the rules.


    OK, Take 2.

    There is way too much code now. Hard to get through.

       Public Event StatusChanged(ByVal e As System.EventArgs)
    

    Is not defined properly. It should have been

       Public Event StatusChanged(ByVal sender as Object,  ByVal e As System.EventArgs)
    

    And, my VB is a bit rusty, next to the “Handles ” notation you should be able to put a statement (somewhere)

     HandlesEvent Me.StatusChangedHandler, someInstrument.Statuschanged  ' very sloppy approx
    

    And then in a class of your choice (the Form maybe)

     Sub StatusChangedHandler(ByVal sender as Object,  ByVal e As System.EventArgs)
        Dim InsConn as InstrumentConnector = sender ' may need a CType() here
        .... 
     End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .NET class library that has a class with a static method.
I have a .NET class that I want to expose as COM object. The
Hi, I have a .net class that contains a Boolean, this class is sent
If I have a .Net class that is not part of any namespace then
I have an ASP.NET VB.NET web project that references a VB.NET class library. I
I have copied class from net which inherits UIImageView. How to put that class
I have a .NET class (for discussion, ClassA) that calls a SQL Server stored
I have a .net test class. In the Initialize method, I create a windsor
I have .NET assembly with ComVisible class. Some days ago (I can find that
I have .NET assembly with one public class and several private classes. I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.