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 17514
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: Rina0 |
last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| | |