469,890 Members | 1,454 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,890 developers. It's quick & easy.

VB.NET multithreading, callbacks, and passing events to the main thread

AMaxin
5
So I have been learning about MultiThreading in VB.NET, and I did up some code based on an example I found HERE

Everything seemed to work wonderfully, EXCEPT when I started to get into performing functions on the main thread that were async callbacks initiated from a child thread. What I found by examining the thread viewer in VS2005, that the returned event I was using in my app, was still executing on my child thread, not on the main thread like I thought it was... I think this is blowing up a certain part of my code and is holding up my project....

So, what I need is a way to raise an event within a tread and have the main thread receive it (with parameters), and respond to it.. (is this where listeners come in?)

If you have any insight you can provide, please do!

Thank you!

Andrew
Sep 21 '07 #1
6 16993
Plater
7,872 Expert 4TB
Hmm.
I accomplish this by having my threads fire an event.
And I have my main thread be the listener for that event.
Sep 21 '07 #2
AMaxin
5
Great

Do you have / know of a good example of this? I have not had much luck with one...

Thanks,

Andrew


Hmm.
I accomplish this by having my threads fire an event.
And I have my main thread be the listener for that event.
Sep 21 '07 #3
Plater
7,872 Expert 4TB
Well I made a class with a public event say:
public event DoinStuffHandler DoinStuff;

And the delegate would be like:
public delegate int DoinStuffHandler(string somestring, int someint);


Then in my main thread I create an instance of this class and assign the event handler:
myclassthing.DoinStuff+=new DoinStuffHandler(myclassthing_DoinStuff);


Then in my class I can fire the event whenever I want, and my main thread will execute the myclassthing_DoinStuff function.
Sep 21 '07 #4
AMaxin
5
Plater,

Thanks again for your response... You see, I thought I was doing that too... I thought that using async callbacks implied that the main thread would actually execute the callback event, but according to my thread viewer, the callback is still on the other thread... which explains why my code is blowing up...

To give you some context as to the specific issue, I have a pair of classes that allow my to dynamically monitor a folder and notify the main app if a new item appears in it... The thread's only job is to take the contextual information given during the init, and provide notifications as to the arrival of new items.

Since I wanted to have multiple monitoring, I created a collection class and put my callback delegate subs there, as this collection class is instantiated on the main thread. The delegate subs also funnel all the callbacks from the multiple classes into a single return callback.

So, In practice, it seems to work alright.. like I said before, except that when the callback executes, it is doing so, on the callback thread, not the main thread...

So the relevant code for the collection class:

Expand|Select|Wrap|Line Numbers
  1. Public Event NewMessage(ByVal p_strMessageID As String, ByVal p_strMessageFolderID As String, ByVal p_strTargetFolderID As String, ByVal p_blnIsSentItem As Boolean)
  2.     Public Event ScanDied(ByVal p_strFolderID As String, ByVal p_strError As String)
  3.  
  4.  Public Function Add(ByVal p_strFolderID As String, ByRef p_objGWManager As Object) As clsCallback
  5.         Dim objNewClass As New clsCallback
  6.  
  7.         'Set Event handler hooks
  8.         AddHandler objNewClass.NewMessage, AddressOf _NewMessage
  9.         AddHandler objNewClass.ScanDied, AddressOf _ScanDied
  10.         'Add parent object reference and attach folder ID
  11.         objNewClass.m_objGWClient = p_objGWManager
  12.         objNewClass.FolderID = p_strFolderID
  13.  
  14.         'Add to collection
  15.         m_colFolders.Add(objNewClass, objNewClass.FolderID)
  16.         Return objNewClass
  17.  
  18.     End Function
  19.  
  20. Private Sub _NewMessage(ByVal p_strMessageID As String, ByVal p_strMessageFolderID As String, ByVal p_strTargetFolderID As String, ByVal p_blnIsSentItem As Boolean)
  21.         'Bounce Message to parent Class
  22.         RaiseEvent NewMessage(p_strMessageID, p_strMessageFolderID, p_strTargetFolderID, p_blnIsSentItem)
  23.     End Sub
  24.  
  25.     Private Sub _ScanDied(ByVal p_strFolderID As String, ByVal p_strError As String)
  26.         'Bounce Message to parent Class
  27.         RaiseEvent ScanDied(p_strFolderID, p_strError)
  28.     End Sub

And the individual class:

Expand|Select|Wrap|Line Numbers
  1. Public Event NewMessage(ByVal p_strMessageID As String, ByVal p_strMessageFolderID As String, ByVal p_strTargetFolderID As String, ByVal p_blnIsSentItem As Boolean)
  2.     Public Event ScanDied(ByVal p_strFolderID As String, ByVal p_strError As String)
  3.  
  4.     Private trd As Thread
  5.  
  6.  Public Sub StartScan()
  7.  
  8.         If mvarintScanStatus <> ScanStatus.Scanning Then
  9.  
  10.             Try
  11.                 'Spawn new thread
  12.                 If mvarblnSentItemsBox = True Then
  13.                     trd = New Thread(AddressOf Scan2)
  14.                 Else
  15.                     trd = New Thread(AddressOf Scan)
  16.                 End If
  17.  
  18.                 trd.IsBackground = True
  19.                 trd.Start()
  20.  
  21.                 mvarintScanStatus = ScanStatus.Scanning
  22.  
  23.             Catch Ex As Exception
  24.  
  25.                 mvarintScanStatus = ScanStatus.HasErrord
  26.             End Try
  27.  
  28.         End If
  29.  
  30.     End Sub
  31.  
  32.  
  33.    Public Sub Scan()
  34.  
  35. ...
  36. ..
  37. ...
  38.                                 If IsInitializing = False Then RaiseEvent NewMessage(strMessageID, mvarstrFolderID, mvarstrTargetFolderID, mvarblnSentItemsBox)
  39. ...
  40. ...
  41. End Sub

I hope you can get the Jist of what I am trying to achieve here...



Thanks again...

Andrew





Well I made a class with a public event say:
public event DoinStuffHandler DoinStuff;

And the delegate would be like:
public delegate int DoinStuffHandler(string somestring, int someint);


Then in my main thread I create an instance of this class and assign the event handler:
myclassthing.DoinStuff+=new DoinStuffHandler(myclassthing_DoinStuff);


Then in my class I can fire the event whenever I want, and my main thread will execute the myclassthing_DoinStuff function.
Sep 21 '07 #5
Plater
7,872 Expert 4TB
Isn't there a FileSystemWatcher class or something that does just that?

I think the point of async callbacks are that the event happens on the foreign thread, but does a pass through over into the main thread?

That's what I got out of using a backgroundworker object (msdn told me to when I wanted many threads to be able to update a textbox)
Sep 21 '07 #6
AMaxin
5
Plater,

Im sure that is the case, but the code is for a workaround for an obtuse email client without a message notification.. So, this (is supposed to) artificially create that..

Anyway, I am open-minded to whatever works... I just hadnt played with that component, mostly because my code has no form on which to put this on.. The application is actually an integration, so I have no main form per se...

So, unless I can somehow extend my class by inheriting the component, I dont see how else it would work...


Andrew


Isn't there a FileSystemWatcher class or something that does just that?

I think the point of async callbacks are that the event happens on the foreign thread, but does a pass through over into the main thread?

That's what I got out of using a backgroundworker object (msdn told me to when I wanted many threads to be able to update a textbox)
Sep 21 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Jean-Yves Nief | last post: by
1 post views Thread by dixp | last post: by
16 posts views Thread by Robert Zurer | last post: by
8 posts views Thread by Michael McDowell | last post: by
6 posts views Thread by Michael | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.