473,769 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 thrDisplayUpdat e 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.
InitializeCompo nent()
Catch ex As Exception
Stop
End Try
' InitialiseSeria lPort()

'Me.CreateGraph ics()
'Add any initialization after the InitializeCompo nent() call

Me.thrComms = New Thread(New ThreadStart(Add ressOf
Me.Thread_232))
Me.thrDisplayUp date = New Thread(New ThreadStart(Add ressOf
Me.Thread_Displ ayUpdate))

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

Me.thrComms.Pri ority = ThreadPriority. AboveNormal
Me.thrDisplayUp date.Priority = ThreadPriority. Normal

Me.thrComms.Sta rt()
Me.thrDisplayUp date.Start()

Me.threadsRunni ng = 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 MethodCallInvok er(AddressOf
interface_read_ Wrapper))
' get 50 messages
If (returned_statu s = HTX_SUCCESS) Then
' do processing
End If

'...
If cycle_count > 50 Then

' reset interface
'Thread.Sleep(1 )
controlInvoker. Invoke(New
MethodCallInvok er(AddressOf interface_stop_ Wrapper))

'Thread.Sleep(1 )
controlInvoker. Invoke(New
MethodCallInvok er(AddressOf interface_start _Wrapper))

cycle_count = 0
End If

Loop While Me.threadsRunni ng

End Sub
Public Sub Thread_display( )
controlInvoker2 = New ControlInvoker( Me)
Do
controlInvoker2 .Invoke(New MethodCallInvok er(AddressOf
update_display_ wrapper))
Loop While Me.threadRunnin g
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.Collecti ons
Imports System.Windows. Forms
Public Delegate Sub MethodCallInvok er(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 MethodCallInvok er
Public arguments() As Object
Public Sub New(ByVal invoker As MethodCallInvok er, ByVal
arguments() As Object)
Me.invoker = invoker
Me.arguments = arguments
End Sub 'New
End Class 'MethodCall

Private control As Control
Private argumentInvokeL ist 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 MethodCallInvok er, ByVal
ParamArray arguments() As Object)

Me.argumentInvo keList.Enqueue( New MethodCall(invo ker,
arguments))

control.Invoke( New EventHandler(Ad dressOf ControlInvoke))

End Sub 'Invoke
Private Sub ControlInvoke(B yVal sender As Object, ByVal e As
EventArgs)
Dim methodCall As MethodCall =
CType(argumentI nvokeList.Deque ue(), MethodCall)
Try
methodCall.invo ker(methodCall. arguments)
Catch ex As Exception
Debug.Assert(Fa lse)

End Try

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

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

Similar topics

11
4269
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 threads of a multithreaded program using embedded SQL. Since the threads do not need to share transaction scopes, the sqleAttachToCtx family of APIs do not seem to be necessary. <quote> In the default implementation of threaded applications against...
16
8509
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
13619
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 want to have a nice progress bar in another form e.g ProgressForm to perform the search action. i tried to do it but the progress bar won't work and the GUI freezed. I knew how to do with the single threading but i don't know how to do it in...
5
2139
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 when I got two threads going at the same time. What I have is two text boxes (textBox1 and textBox2) and four buttons(cmdStartThread1, cmdStartThread2, cmdStopThread1, cmdStopThread2)
2
2312
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 I just wanted to ask if I am missing anything based on the following scenario. My test app pulls data from a large external data source which has a table-like structure (but not rdbms - more
55
3325
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 call operations on Controls from a delegate, otherwise it does not work. However each time I've done an operation, I must update the progressbar and progresslabel, but this cannot be done in the delegate as it does not work.
5
2488
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 multithreading online using C# .
2
2264
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 divided the complete drawing into 3 parts..1st will be done by main thread and other two are done in these procedures - <1LongTimeTask <2LongTimeTask2 I have invoked the threads using below method. **************
7
16312
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 Exceptional C++, for example). Platform-specific (e.g.: Win32, POSIX) is OK, as long as it's good :) Thank you, Ray
7
4493
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. Each factory, implemented as a thread, can create only one type of the objects below: wheels, windows, doors, chassis or engines. From the GUI an user should be able to add a new factory or delete an existing one by specifying its type (eg. engine...
0
9422
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10206
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10035
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9984
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8863
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7403
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.