472,779 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 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 17514
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: 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...
0
linyimin
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...
0
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...
0
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...
5
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...
0
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=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
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...

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.