473,396 Members | 2,089 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.

multithreading problem - help!

Hi all,

I'm trying to program a multithreaded application - it currently
works, but I have two problems. I'm relatively new to VB.NET (C and
VB6 are my things) so I'm a bit lost here. The code is based on the
countdown / controlinvoker quickstart at GotDotNet.com.

The problems are:

(1) There's a memory leak somewhere. I don't know where, but it's
probably something stupid I've done when I added a second thread.

(2) How do I set it up to exit gracefully (i.e. where in the
application should I set threadsRunning = FALSE); at the moment even
when in debug mode it crashes when stopping - I assume because I
haven't sewn things up properly.

The basics of the thread architecture are below. I've added a second
thread to the quickstart code, and I've added a second ControlInvoker
instance. With regard to the two threads, one updates the screen and
the other communicates with a data comms interface, and passes some of
this data to the serial port. The communications routine should take
priority over the screen updating.

Can anyone cast an expert eye over it and tell me where I'm being
thick?

Thanks
Andy

===========================

' main application class:
Public Class Form1
Inherits System.Windows.Forms.Form
Private thrComms As Thread
Private thrDisplayUpdate As Thread
Private threadsRunning As Boolean
Private controlInvoker As controlInvoker
Private controlInvoker2 As controlInvoker

' ...

Public Sub New()

MyBase.New()

Try
'This call is required by the Windows Form Designer.
InitializeComponent()
Catch ex As Exception
Stop
End Try
' InitialiseSerialPort()

'Me.CreateGraphics()
'Add any initialization after the InitializeComponent() call

Me.thrComms = New Thread(New ThreadStart(AddressOf
Me.Thread_232))
Me.thrDisplayUpdate = New Thread(New ThreadStart(AddressOf
Me.Thread_DisplayUpdate))

controlInvoker = New ControlInvoker(Me)
controlInvoker2 = New ControlInvoker(Me)

Me.thrComms.Priority = ThreadPriority.AboveNormal
Me.thrDisplayUpdate.Priority = ThreadPriority.Normal

Me.thrComms.Start()
Me.thrDisplayUpdate.Start()

Me.threadsRunning = True
End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
MyBase.Dispose(disposing)
End Sub
' ....

Public Sub Thread_232()
' Data comms worker thread

Dim rxmsg_counter As Integer
Dim messages As sPCCanMsg

Dim temp_sec_value As Integer

controlInvoker = New ControlInvoker(Me)
'worker thread

Do
msg_list_size = 50
controlInvoker.Invoke(New MethodCallInvoker(AddressOf
interface_read_Wrapper))
' get 50 messages
If (returned_status = HTX_SUCCESS) Then
' do processing
End If

'...
If cycle_count > 50 Then

' reset interface
'Thread.Sleep(1)
controlInvoker.Invoke(New
MethodCallInvoker(AddressOf interface_stop_Wrapper))

'Thread.Sleep(1)
controlInvoker.Invoke(New
MethodCallInvoker(AddressOf interface_start_Wrapper))

cycle_count = 0
End If

Loop While Me.threadsRunning

End Sub
Public Sub Thread_display()
controlInvoker2 = New ControlInvoker(Me)
Do
controlInvoker2.Invoke(New MethodCallInvoker(AddressOf
update_display_wrapper))
Loop While Me.threadRunning
End Sub
Private Sub update_display_wrapper(ByVal arguments() As Object)
'----- Start the Display task
' Gets data from Global Variables, scales data and updates the screen.

' ......do lots of screen updating....

End Sub
Private Sub interface_start_wrapper(ByVal arguments() As Object)
canstatus2 = InterfaceStart(1) ' DLL call to start the data comms
interface
End Sub
Private Sub interface_stop_wrapper(ByVal arguments() As Object)
canstatus2 = InterfaceStop(1)' DLL call to stop the data comms
interface
End Sub

Private Sub interface_read_wrapper(ByVal arguments() As Object)
canstatus2 = InterfaceReadEx(1, msg_list(0), 5, 50)' DLL call to
read 50 messages from data comms interface
End Sub

End Class

=====================================

'''' CONTROLINVOKER CLASS ''''

Imports System
Imports System.Collections
Imports System.Windows.Forms
Public Delegate Sub MethodCallInvoker(ByVal o() As Object)

' Control.Invoke allows a method to be invoked on the same thread as
the one
' the control was created on. Unlike in the full .NET Framework, the
..NET
' Compact Framework does not support the Control.Invoke overload for
passing an
' array of objects to pass as arguments. This ControlInvoker class
overcomes
' this limitation.
Public Class ControlInvoker

Private Class MethodCall
Public invoker As MethodCallInvoker
Public arguments() As Object
Public Sub New(ByVal invoker As MethodCallInvoker, ByVal
arguments() As Object)
Me.invoker = invoker
Me.arguments = arguments
End Sub 'New
End Class 'MethodCall

Private control As Control
Private argumentInvokeList As New Queue
' The constructor typically takes a form
Public Sub New(ByVal control As Control)
Me.control = control
End Sub 'New
' The delegate wrapping the method and its arguments
' to be called on the same thread as the control.
Public Sub Invoke(ByVal invoker As MethodCallInvoker, ByVal
ParamArray arguments() As Object)

Me.argumentInvokeList.Enqueue(New MethodCall(invoker,
arguments))

control.Invoke(New EventHandler(AddressOf ControlInvoke))

End Sub 'Invoke
Private Sub ControlInvoke(ByVal sender As Object, ByVal e As
EventArgs)
Dim methodCall As MethodCall =
CType(argumentInvokeList.Dequeue(), MethodCall)
Try
methodCall.invoker(methodCall.arguments)
Catch ex As Exception
Debug.Assert(False)

End Try

End Sub 'ControlInvoke
End Class 'ControlInvoker
Nov 20 '05 #1
0 1499

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
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
9
by: Popoxinhxan | last post by:
Dear experts, i want to develop an client application that consume the google search web service. In my MainForm i have a method to retrieve all the search result e.g. GetGoogleResults(). Now i...
5
by: sarge | last post by:
I would like to know how to perform simple multithreading. I had created a simple form to test out if I was multithreading properly, but got buggy results. Sometime the whole thig would lock up...
2
by: Rich | last post by:
Hello, I have set up a multithreading routine in a Test VB.net proj, and it appears to be working OK in debug mode and I am not using synchronization. Multithreading is a new thing for me, and...
55
by: Sam | last post by:
Hi, I have a serious issue using multithreading. A sample application showing my issue can be downloaded here: http://graphicsxp.free.fr/WindowsApplication11.zip The problem is that I need to...
5
by: sandy82 | last post by:
Whats actuallly multithreading is ... and how threading and multithreading differ . Can any1 guide how multithreading is used on the Web .. i mean a practical scenario in which u use...
2
by: Pradnya Patil | last post by:
hi , I am trying to draw ' html div-tag ' on the screen which will resemble a rectangle through vb.net code. I want it to be drawn faster...so I introduced multithreading using Threadpool. I...
7
by: Ray | last post by:
Hello, Greetings! I'm looking for a solid C++ multithreading book. Can you recommend one? I don't think I've seen a multithreading C++ book that everybody thinks is good (like Effective C++ or...
7
darlene
by: darlene | last post by:
Hi, I need some help in creating an application in Visual C++ which should make use of MFC and multithreading. The application is supposed to consist in a number of threads representing factories....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.