| | |
Threads and shared data problem
Please support our VB.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
![]() |
•
•
Join Date: Dec 2008
Posts: 2
Reputation:
Solved Threads: 0
Hello,
Firstly your forum has solved many of my questions in the past, I have had to register to find the solution to this one.
The current application I am working on is multi-threaded and I am really getting confused with VB.NET's way of doing things (ex Java programmer).
Basically,
A schedule class runs every 60 seconds querying our database for new "Job's" to do. If it finds "Job's" it spawns a Job thread for each job and waits another 60 seconds.
A Job class fetches required "Report's" of the "Job" and spawns a "Report" thread for each of the "Reports". (Note: a report thread can take 1 sec to 20 sec to run)
My problem is, I have implemented a delegate sub in the "Job" class that the "Report" class threads use. Leading to my problem that when two "Job" threads are running the "Report" threads aren't talking back to the correct "Job" thread. The "Report" threads need to pass back the processed data to the "Job" thread for merging.
Are delegates the best way to handle communication between my threads. The examples I have seen the Delegate subs or functions have been in the same class. My problem is that I have had to make it Shared in the Job class so the Report class can see it without having to make an instance of the Job class.
(I would post code, but its a lot of code. Will post snippets soon once I can make it simpler to understand)
Summary:
Schedule (Polls database for Jobs)
Job (Handles the Job from the database)
Report (Handles the Report part of the Job)
Firstly your forum has solved many of my questions in the past, I have had to register to find the solution to this one.
The current application I am working on is multi-threaded and I am really getting confused with VB.NET's way of doing things (ex Java programmer).
Basically,
A schedule class runs every 60 seconds querying our database for new "Job's" to do. If it finds "Job's" it spawns a Job thread for each job and waits another 60 seconds.
A Job class fetches required "Report's" of the "Job" and spawns a "Report" thread for each of the "Reports". (Note: a report thread can take 1 sec to 20 sec to run)
My problem is, I have implemented a delegate sub in the "Job" class that the "Report" class threads use. Leading to my problem that when two "Job" threads are running the "Report" threads aren't talking back to the correct "Job" thread. The "Report" threads need to pass back the processed data to the "Job" thread for merging.
Are delegates the best way to handle communication between my threads. The examples I have seen the Delegate subs or functions have been in the same class. My problem is that I have had to make it Shared in the Job class so the Report class can see it without having to make an instance of the Job class.
(I would post code, but its a lot of code. Will post snippets soon once I can make it simpler to understand)
Summary:
Schedule (Polls database for Jobs)
Job (Handles the Job from the database)
Report (Handles the Report part of the Job)
•
•
Join Date: Dec 2008
Posts: 2
Reputation:
Solved Threads: 0
(Sorry for the double post, the edit button has gone)
Here is the code, the problem is illustrated when ran, the Job class expects 3 returns but the "Report" thread return to both classes (6 rather than 3).
I need a way for the report threads to only report to their job thread and not any others running!
(Used to run it as a console application)
The schedule class
The Job class
The Report class
Here is the code, the problem is illustrated when ran, the Job class expects 3 returns but the "Report" thread return to both classes (6 rather than 3).
I need a way for the report threads to only report to their job thread and not any others running!
(Used to run it as a console application)
VB.NET Syntax (Toggle Plain Text)
Module Module1 Sub Main() Console.WriteLine("(Module) Running schedule prototype") Dim schedule As New ScheduleClass() schedule.run() End Sub End Module
The schedule class
VB.NET Syntax (Toggle Plain Text)
Imports System.Threading Public Class ScheduleClass Public Sub run() Try ' Thread name counter Dim thread_counter As Integer = 0 While True ' Simulate 2 jobs at once newJobThread("Job Thread " & thread_counter.ToString) newJobThread("Job Thread " & thread_counter.ToString + 1) thread_counter = thread_counter + 1 Thread.Sleep(60000) End While Catch ex As Exception End Try End Sub Private Sub newJobThread(ByVal thread_name As String) Dim job As New JobClass(thread_name) Dim thread As New Thread(AddressOf job.run) thread.Name = thread_name thread.Priority = ThreadPriority.Normal thread.Start() End Sub End Class
The Job class
VB.NET Syntax (Toggle Plain Text)
Imports System.Threading Public Class JobClass Dim thread_name As String Shared job_data As ArrayList Public Sub New(ByVal thread_name As String) Me.thread_name = thread_name job_data = New ArrayList(3) Console.WriteLine("(JobClass) New thread with name: " & Me.thread_name) End Sub Public Sub run() ' Simulate generating 3 reports for the job thread newReportThread(Me.thread_name & ": Report Thread 1") newReportThread(Me.thread_name & ": Report Thread 2") newReportThread(Me.thread_name & ": Report Thread 3") While job_data.Count <> 3 Console.WriteLine("(JobClass) Not all data returned (Only " & job_data.Count & " out of 3)") Thread.Sleep(3000) End While Console.WriteLine("(JobClass) All data returned now outputing for thread: " & Me.thread_name) Dim loop_counter As Integer = 0 While loop_counter < job_data.Count Console.WriteLine("(JobClass) Item(" & loop_counter & ") = " & job_data.Item(loop_counter).ToString) loop_counter = loop_counter + 1 End While End Sub Private Sub newReportThread(ByVal thread_name As String) Dim report As New ReportClass(Me.thread_name & ":" & thread_name) Dim thread As New Thread(AddressOf report.run) thread.Name = Me.thread_name & ":" & thread_name thread.Priority = ThreadPriority.Normal thread.Start() End Sub Public Shared Sub returnReportThreadData(ByVal data As String) SyncLock (job_data) job_data.Add(data) End SyncLock End Sub End Class
The Report class
VB.NET Syntax (Toggle Plain Text)
Imports System.Threading Public Class ReportClass Dim thread_name As String Public Sub New(ByVal thread_name As String) Me.thread_name = thread_name Console.WriteLine("(ReportClass) New Report thread: " & Me.thread_name) End Sub Public Sub run() Dim handler As New returnData(AddressOf JobClass.returnReportThreadData) Dim rand As New Random ' Simulate waiting for the report to be completed Thread.Sleep(1000 * rand.Next(1, 5)) handler.Invoke("Hi from thread: " & Me.thread_name) End Sub Delegate Sub returnData(ByVal data As String) End Class
![]() |
Similar Threads
- Help needed for IE6/Netscape problems (Web Browsers)
- start up problem. please help (Viruses, Spyware and other Nasties)
- Send data on a serial port (C++)
- problem with threads :( (Java)
- another newbie with alot of redhat and apache server Q'S (Linux Servers and Apache)
- Playing with shared memory - problem with non 'simple' data types (C++)
- Error 317, Stealth.Hjack Virus (Viruses, Spyware and other Nasties)
- Sorry, another "bridge.dll" problem.. here's my HJT log. (Viruses, Spyware and other Nasties)
- Need Help for DNS Problem and 'about:blank' Problem ... (Viruses, Spyware and other Nasties)
Other Threads in the VB.NET Forum
- Previous Thread: how to find that a date exists in between two dates ?
- Next Thread: Teach Me a Comeback to Visual Basic.net Programming
| Thread Tools | Search this Thread |
.net .net2008 2005 2008 access account add application array basic beginner browser button buttons center check click code combo cpu crystalreport cuesent database datagrid datagridview date datetimepicker designer dissertation dissertations dissertationtopic dropdownlist excel exists fade file-dialog filter forms ftp generatetags html images input insert intel listview mobile module monitor mysql net number open output panel passingparameters picturebox picturebox2 port print printing printpreview problem regex reuse right-to-left searchvb.net select settings shutdown socket sqldatbase sqlserver storedprocedure survey tcp temperature textbox timespan transparency trim txttoxmlconverter user usercontol vb vb.net vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet vista visual visualbasic.net visualstudio.net visualstudio2008 web winforms wpf wrapingcode xml year





