Time for a threading question

  • Thread starter Thread starter CJ Taylor
  • Start date Start date
C

CJ Taylor

Hey Everyone,

Alright, now I cannot get this to work, I may be dumb and don't see it, but
sickness will do that every now and then.

So I have a program with 4 threads performing unique operations. Syncing
isn't really important in this, just 4 different watches that happen.

So anyways, these "watchers" are on these threads, all raise an event called
LogEvent to the main thread. LogEvent is in a module so it can be called
from anywhere (as long as its imported of course).

However, I want to hook into the shared event of LogEvent (called Notify) on
my main thread, and take some of the information that happens from this
shared event and post it on a form.

However, I keep getting an error that it can't invoke across threads.
However, I think I have done everything, but maybe I'm missing something.

here is the event handler code.

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

AddHandler Logger.EventLogger.Notify, AddressOf EvtLog

End Sub

Private Sub EvtLog(ByVal sender As Object, ByVal e As Logger.LogEventArgs)



Dim tv_Node As TreeNode

tv_Node = New TreeNode(e.EventDate)

tv_Node.Tag = e.Source

tv_Node.Nodes.Add(e.Source)

' Me.tv_Events.Nodes.Add(tv_Node)

End Sub
 
WOW!!! Thats, Thats, Thats, WOW!!!
Wise man once said "keep it simple, stupid!"
OK, No Realy. If you need to use 4 threads then go for it
but i wouldent recommend distributing that. you could use
array and a variable and when the variable is changed
then update your treeview with the array.
 
Hi CJ,

Do you remember that Topic: newbie thread mishap that you replied to
earlier today? Herfried and I posted there as well. Your problem may be
similar. Your solution may be the same.

Hope so anyway. :-)

Regards,
Fergus
 
Hey Fregus,

Yea I saw that, however didn't quite explain it. I think what I'm looking
for is an explanation of how control.Invoke works. Or at least a little
more about what that means in respects to a class.

I have a class that spins off the threads (which I don't appreciate someone
calling me "stupid" for no reason. If I could have used arrays, I would
have, but I couldn't because of time issues... jacka**). anyways, these
threads raise an event logger, in which case I had a handler in my main
thread to handle anytime this shared event was fired.

So I have a DLL called Logger.dll, all shared properties/methods. Different
classes/components call this dll and its methods, primarly logEvent (sender
as object, e as LogEventArgs).

There is a public shared event called Notify, in which the main form
(frmMain) monitors. Now this works fine, the events fire, etc. However, if
you fire from another thread (IE, thread 2 calls LogEvent, which fires
Notify) it says there is an error about crossing threads and to use
control.invoke instead.

Thats what is confusing me. Does this make anymore sense?

thanks,
CJ
 
Hi Chris,

Wow was using the full version of an acronym KISS that is often used in
training companies. "keep it simple, stupid!". I hate it - the entire
usefulness of the concept just gets wiped away by the insult at the end. ;-)

The whole problem does make more sense. It may be useful to see your
thread-spinner class and one of these threads (assuming the 4 are similar). If
possible, maybe you could post a project? If you don't want to post it here,
you may send it direct - my email is valid.

I'm going out right now and will be back in a few hours. I'd like to look
into this in depth and get a decent grasp of the concepts here.

Someone may have answered you in the meantime but in the words of Arnie..

"I'll be back"

Regards,
Fergus
 
Alright, So I've been screwing with this using method invoker to execute a
method on an underlying window handle.

However I have ...

Private Sub EvtLog(ByVal sender As Object, ByVal e As Logger.LogEventArgs)

Dim tv_Node As TreeNode

tv_Node = New TreeNode(e.EventDate)

tv_Node.Tag = e.Source

tv_Node.Nodes.Add(e.Source)

' Me.tv_Events.Nodes.Add(tv_Node)

Me.tv_Events.Invoke(Nodes.Add, tv_Node)

End Sub

And I'm getting an error on Nodes.Add in the me.tv_Events.Invoke saying that
Nodes is private and cannot invoke the Add command. Suggestions?



Thanks,

CJ
 
Never mind... got it...


CJ Taylor said:
Alright, So I've been screwing with this using method invoker to execute a
method on an underlying window handle.

However I have ...

Private Sub EvtLog(ByVal sender As Object, ByVal e As Logger.LogEventArgs)

Dim tv_Node As TreeNode

tv_Node = New TreeNode(e.EventDate)

tv_Node.Tag = e.Source

tv_Node.Nodes.Add(e.Source)

' Me.tv_Events.Nodes.Add(tv_Node)

Me.tv_Events.Invoke(Nodes.Add, tv_Node)

End Sub

And I'm getting an error on Nodes.Add in the me.tv_Events.Invoke saying that
Nodes is private and cannot invoke the Add command. Suggestions?



Thanks,

CJ



Notify)
 
Delegate Sub AddEventNodeDelegate (ByVal e As Logger.LogEventArgs)

Private Sub EvtLog(ByVal sender As Object, ByVal e As Logger.LogEventArgs)
tv_Events.Invoke(new AddEventNodeDelegate(AddressOf AddEventNode), New
Object() {e})
End Sub


Private Sub AddEventNode (ByVal e as Logger.LogEventArgs)

Dim tv_Node As TreeNode

tv_Node = New TreeNode(e.EventDate)
tv_Node.Tag = e.Source

tv_Node.Nodes.Add(e.Source)

tv_Events.Nodes.Add(tv_Node)

End Sub
 
Back
Top