0

I have an application I have developed that I will break down into 'simpler' terms just to explain where I am experiencing the problem.

The application has a BackGroundWorker with DoWork, Completed and ProgressChanged Methods. This are located in the 'Main' Class, I then have a 'ScheduledSend' Class that contains code with TimedEvent Handler so that when Time = X it executes the RunWorkerASync for the BGW.

This is because I handle the application both by a forced button press or an out of hours 'scheduled' execute.

I then have a RichTextBox located on the 'Main' Form to display Log information as the process is running.

The Problem When the application is ran using the forced button, the rich textbox log is updated correctly, however when it runs using the scheduled send it does not update at all, can someone please explain to me why this is the case.

I have detailed the code in 'simpler' terms below.

Public Class Main

'Timer Variables
Private Shared aTimer As System.Timers.Timer
Private Shared aTimerTime As Integer

Private Sub Main_Load(sender As Object, e As EventArgs) Handles Me.Load

'Setup the Scheduled Send Timers
        aTimerTime = (1000)

        aTimer = New System.Timers.Timer(aTimerTime) '1000 = 1 second, 60000 = 1 minute

        AddHandler aTimer.Elapsed, AddressOf ScheduledSend.OnTimedEvent

        aTimer.Enabled = True



End Sub

Private Sub SendManFileBtn_Click(sender As Object, e As EventArgs) Handles SendManFileBtn.Click

    Try

        LogFileRTB.Text = ""

        TSProgBar.MarqueeAnimationSpeed = 100

        BGW1BuildXML.RunWorkerAsync()

    Catch ex As Exception
        ErrorLogging.ReportError(ex)
    End Try


 End Sub

 Private Sub BGW1BuildXML_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BGW1BuildXML.ProgressChanged
    Try

        LogFileRTB.AppendText(DateTime.Now.ToLongTimeString & "     " & e.UserState.ToString & Environment.NewLine)

    Catch ex As Exception
        ErrorLogging.ReportError(ex)
    End Try

 End Sub

 End Class

Public Class ScheduledSend

Public Shared Sub OnTimedEvent(ByVal source As Object, ByVal e As ElapsedEventArgs)

    Try

        Console.WriteLine(e.SignalTime.ToLongTimeString)
        Console.WriteLine(My.Settings.schTimer.ToLongTimeString)

        If e.SignalTime.ToLongTimeString = My.Settings.schTimer.ToLongTimeString Then
            Console.WriteLine("Scheduled Timer Activated...")
            Main.BGW1BuildXML.ReportProgress(0, "Scheduled Timer Activated...")
            Main.BGW1BuildXML.RunWorkerAsync()
        End If

    Catch ex As Exception
        ErrorLogging.ReportError(ex)
    End Try

End Sub

End Class

So, I have added the Main Form Load, this is where it sets up the timers and such, the Btn Click that 'forces' the process which uses BGWReportProgress correctly and updates the RichTextBox via the ReportProgress Method.

Then the 'ScheduledSend' Class has the 'Timed' Method that when the times of 2 variables match it launches, this DOESN'T update the RichTextBox correctly, the following line:

Main.BGW1BuildXML.ReportProgress(0, "Scheduled Timer Activated...")

Leaves the RichTextbox Blank.

Any help would be appreciated.

Lynchie
  • 1,077
  • 2
  • 20
  • 36
  • This seems to be a backfire from the ['default form instance'](http://stackoverflow.com/questions/4698538/why-is-there-is-a-default-instance-of-every-form-in-vb-net-but-not-in-c) feature. How does your ScheduledSend class know which Main form instance contains the RichTextBox to update? – Steve Dec 30 '15 at 10:52
  • @Steve In the 'ProgressChanged' I literally just reference the RichTextBox with LogFileRTB.AppendText(DateTime.Now.ToLongTimeString & " " & e.UserState.ToString & Environment.NewLine) – Lynchie Dec 30 '15 at 10:55
  • I know but you seem to be calling this from on the default form instance not from the actual main instance. I would try to add a public shared property to your ScheduleSend class of the same type of your Main form. Then when you are in Main_Load set the property to the current instance (Me) in the OnTimedEvent use that property when calling the ReportProgress – Steve Dec 30 '15 at 10:58
  • 2
    The OnTimedEvent() method runs on a worker thread. It creates a **new** instance of the Main class and the BGW updates that one. Not the one you are looking at. Unlearning to use the horrid default instance feature does take a brain transplant, hard to get one at SO. There is no point whatsoever in using System.Timers.Timer, get ahead by using a regular Winforms Timer and its Tick event. – Hans Passant Dec 30 '15 at 11:05
  • So do I need to 'Unlearn' the default instance feature to get this to work or will just changing my System.Timers.Timer to a regular Winforms Timer and Tick Event solve the issue? Cheers for the advice gentlemen. – Lynchie Dec 30 '15 at 11:09
  • @HansPassant I used a WinForms Timer and Tick Event within my 'Main' Instance and this has rectified the matter, I'm guessing due to your 'Creating Of New Instance' with the OnTimedEvent. – Lynchie Dec 30 '15 at 11:34

0 Answers0