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
6 17678
Hmm.
I accomplish this by having my threads fire an event.
And I have my main thread be the listener for that event.
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.
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.
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: - Public Event NewMessage(ByVal p_strMessageID As String, ByVal p_strMessageFolderID As String, ByVal p_strTargetFolderID As String, ByVal p_blnIsSentItem As Boolean)
-
Public Event ScanDied(ByVal p_strFolderID As String, ByVal p_strError As String)
-
-
Public Function Add(ByVal p_strFolderID As String, ByRef p_objGWManager As Object) As clsCallback
-
Dim objNewClass As New clsCallback
-
-
'Set Event handler hooks
-
AddHandler objNewClass.NewMessage, AddressOf _NewMessage
-
AddHandler objNewClass.ScanDied, AddressOf _ScanDied
-
'Add parent object reference and attach folder ID
-
objNewClass.m_objGWClient = p_objGWManager
-
objNewClass.FolderID = p_strFolderID
-
-
'Add to collection
-
m_colFolders.Add(objNewClass, objNewClass.FolderID)
-
Return objNewClass
-
-
End Function
-
-
Private Sub _NewMessage(ByVal p_strMessageID As String, ByVal p_strMessageFolderID As String, ByVal p_strTargetFolderID As String, ByVal p_blnIsSentItem As Boolean)
-
'Bounce Message to parent Class
-
RaiseEvent NewMessage(p_strMessageID, p_strMessageFolderID, p_strTargetFolderID, p_blnIsSentItem)
-
End Sub
-
-
Private Sub _ScanDied(ByVal p_strFolderID As String, ByVal p_strError As String)
-
'Bounce Message to parent Class
-
RaiseEvent ScanDied(p_strFolderID, p_strError)
-
End Sub
And the individual class: - Public Event NewMessage(ByVal p_strMessageID As String, ByVal p_strMessageFolderID As String, ByVal p_strTargetFolderID As String, ByVal p_blnIsSentItem As Boolean)
-
Public Event ScanDied(ByVal p_strFolderID As String, ByVal p_strError As String)
-
-
Private trd As Thread
-
-
Public Sub StartScan()
-
-
If mvarintScanStatus <> ScanStatus.Scanning Then
-
-
Try
-
'Spawn new thread
-
If mvarblnSentItemsBox = True Then
-
trd = New Thread(AddressOf Scan2)
-
Else
-
trd = New Thread(AddressOf Scan)
-
End If
-
-
trd.IsBackground = True
-
trd.Start()
-
-
mvarintScanStatus = ScanStatus.Scanning
-
-
Catch Ex As Exception
-
-
mvarintScanStatus = ScanStatus.HasErrord
-
End Try
-
-
End If
-
-
End Sub
-
-
-
Public Sub Scan()
-
-
...
-
..
-
...
-
If IsInitializing = False Then RaiseEvent NewMessage(strMessageID, mvarstrFolderID, mvarstrTargetFolderID, mvarblnSentItemsBox)
-
...
-
...
-
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.
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)
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)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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
|
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...
|
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?...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| | |