473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 17678
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

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

Similar topics

0
by: Jean-Yves Nief | last post by:
hello, I have written a script which is performing some tasks in multithreading mode: the main thread is opening a connection to a distant server and all the threads that I start will have to...
1
by: dixp | last post by:
I'm new to writing multithreaded apps and I have a design question. I have a winforms app and a class which has a method that does processing which is time intensive. I want the user to be able...
16
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
8
by: Michael McDowell | last post by:
I'm confused: "why do we need to assign a method to a delegate then assign the delegate to an event why not just assign the method to the events event handler" and "how does making a...
6
by: VM | last post by:
I'm trying to add multithreading to my win application but I'm having trouble with the code since the method to be threaded has parameters. How can I add multithreading to a method with parameters?...
2
by: SStory | last post by:
Here is the situation. I want to display Icons, Type of file etc from a file extension. Upon initial program load I may only need icons for certain files. But other operations will require...
20
by: Charles Law | last post by:
Consider the following scenario: A data packet is sent out of a serial port and a return packet is expected a short time later. The application sending the packet needs to send another packet...
6
by: Michael | last post by:
I have an application that monitoring some directories. When new file arrives my application importing that file into database (very long process) I want to rewrite this application to multithread...
6
by: MeowCow | last post by:
I will try and make my question with out being too long winded. I have been doing a lot of reading on how to do multithreading and I have implemented the code from the following example on...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.