Public Class EventLogger #Region " Events " Public Event EntryWritten As EntryWrittenEventHandler Public Delegate Sub EntryWrittenEventHandler(x As String) #End Region #Region " Methods " Sub WriteEntry([Text] As String) Try My.Computer.FileSystem.WriteAllText("D:\log.txt", [Text], True) RaiseEvent EntryWritten([Text]) Catch ex As Exception Debug.Print(ex.Message) End Try End Sub #End Region End Class
Public Class frmMain Public Logger As New EventLogger Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load AddHandler Logger.EntryWritten, AddressOf OnEntryWritten Logger.WriteEntry("Application initialized.") 'This Works Threading.ThreadPool.QueueUserWorkItem(AddressOf Start) End Sub Private Sub OnEntryWritten(x As String) EventLog.Text = x End Sub Sub Start() 'Cross-thread operation not valid: Control 'EventLog' accessed from a thread other than the thread it was created on. Logger.WriteEntry(-1, "hello") End Sub End Class
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)