Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello everyone, im really sorry if i did anything wrong, i just don't know how to make this thing works !

So everyone here's the class i made it to store the logs into TextBox / txt.

VB
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


Usage :

VB
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


Anyone can help me ?

Thank you everyone.
Posted
Updated 24-Apr-14 13:00pm
v3
Comments
Dave Kreskowiak 24-Apr-14 18:29pm    
Why are you even raising an event?
Sergey Alexandrovich Kryukov 24-Apr-14 18:42pm    
Good question. The purpose is really unclear, as well as the whole question.
I answered in a very general but detailed way, please see.
—SA
nazarinlaw 24-Apr-14 18:49pm    
I raised an event to update my textbox bro !
Sergey Alexandrovich Kryukov 24-Apr-14 19:19pm    
It does not explain it. To update something, you just need to Invoke it.
—SA
Dave Kreskowiak 24-Apr-14 23:57pm    
First, don't call anyone you don't know "bro".

Second, in what you've posted, there's no reason you need an event at all. You just need to Invoke a function on the UI thread to update the TextBox.

As a side, if your log has more than a few messages in it, the TextBox is a terrible control to use. The string you keep appending to is being reallocated over and over again each time you log a message. Use a ListBox instead.

1 solution

There is no such concept as "invoke in class". This is irrelevant. It's important in what thread you are running some code. There are two very different cases: if you need to invoke some operation to a UI thread, or to do it in some custom thread. I answered both question in detail in my past answer:
.NET event on main thread[^].

For the detailed explanation of the mechanisms involved, see my article referenced in that answer with some comments on it. Please see: Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

—SA
 
Share this answer
 
Comments
nazarinlaw 24-Apr-14 18:58pm    
Sorry im rejecting this answer for just getting your attention ! please don't take this wrong :(
Sergey Alexandrovich Kryukov 24-Apr-14 19:19pm    
No problem at all, but I still think this is all you really need. You just need to use this information properly.
If you have any follow-up question, you are quite welcome to ask them.
—SA
nazarinlaw 24-Apr-14 19:51pm    
Thank you bro, please look at the code below and let me know is it correct or do i have to lock the object to perform the threads to update my textbox without any further issue ?
nazarinlaw 24-Apr-14 19:45pm    
I think that i figured it out bro ! please tell me if im wrong :)

Public Class EventLogger

#Region " Methods "

Public Target As Control
Sub AssignControl(c As Control)
Target = c
End Sub

Private Delegate Sub WriteEntryDelegate(Index As Integer, [Text] As String)
Sub WriteEntry(Index As Integer, [Text] As String)
Try
If Target.InvokeRequired Then
Target.Invoke(New WriteEntryDelegate(AddressOf WriteEntry), Index, [Text])
Else
Target.Text &= "Thread: " & Index & [Text]
End If
Catch ex As Exception
Debug.Print(ex.Message)
End Try
End Sub

#End Region


End Class
Sergey Alexandrovich Kryukov 24-Apr-14 21:29pm    
More or less...
Let's see:

AssignControl is pointless and Target is bad style. Should be, say, private-write public-read property instead (or whatever you need). Public fields means bad style, should be properties. Pretty obvious why.

InvokeRequired is only needed if called from the thread rather then UI. If this is the UI thread, no invoke needed. I have no idea in what thread are you going to call it...

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900