Solved

How to Prevent Exceptions When Using the Same Method of a Class That's Instantiated in Multiple Threads

Posted on 2014-01-28
9
387 Views
Last Modified: 2014-02-06
I am using a Class's method concurrently in multiple threads, and getting (obviously) exceptions on access.  For example, in one thread I will instantiate as follows:

Dim myobj1 as New MyClass
call myobj1.dosomething(param1,param2)

Open in new window


and then in another thread (immediatelt after first thread), I will use the same syntax:

Dim myobj1 as New MyClass
call myobj1.dosomething(param1,param2)

Open in new window

How can I prevent conflicts when using the method within the class that is nevertheless instantiated in each thread.   Apparently, even though you instantiate a class that has methods, the methods can conflict if they are accessed from a different instantiation of the same class(?)

Should I instead send a parameter to the New method of the class and then call the method that way (not sure this will help?)
Dim methodtouse As String
methodtouse = "dosomething"
Dim myobj1 as New MyClass(methodtouse,param1,param2)

Class MyClass

     Sub New(methodtouse,param1,param2)
        If methodtouse = "dosomething" Then 
           Call dosomething(param1, param2)
        End If
     End Sub

     Sub dosomething(param1, param2)
         ... do work
     End Sub
End Class

Open in new window

0
Comment
Question by:lep1
  • 5
  • 4
9 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39815457
Is the method accessing any variables within the class? Or is it calling any other methods which may be accessing variables?
0
 

Author Comment

by:lep1
ID: 39815552
Yes, but the exception occurred during a bmp.save after creating a graphic bitmap.

The error was an unhandled exception.
0
 

Author Comment

by:lep1
ID: 39815562
Should I check that New is used for creating the bitmap and the graphic?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39816318
Is the bitmap declared within the method? Are you saving the form to a bitmap?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:lep1
ID: 39816439
Yes bitmap is declared in the method.  I then draw image via GDI+, and image is linked to bitmap via FromImage(bmp), the exception occurred at the line with bmp.save.

FYI - this code works successfully and this exception reflects instability - since it first appeared today during multithreaded instantiation of this particular class ( method).
0
 

Author Comment

by:lep1
ID: 39816459
Would it be due to multiple bitmaps (instantiated in different objects ( classes) somehow arriving or going through some pipeline that can only handle saving one bitmap at a time.  In other words, is there some behind the scenes global public image resource that is throwing exceptions since multiple bitmaps are going through it at the same time - when by design it can only handle one at a time?  If this is true the OOP with the bmp.save is not really OOP.  I would not be surprised if there was a bottleneck that is choking since too many bitmaps show up at the same time.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39817338
Can you show your class code?
0
 

Author Comment

by:lep1
ID: 39817967
Sure, here is the method code,  The exception seems random and is a GDI+ exception.

        Dim rect As Rectangle = New Rectangle(0, 0, rwidth, rheight)
        Dim bmp As Bitmap = New Bitmap(rect.Width, rect.Height)
        bmp.SetResolution(displayres, displayres)
        Dim gr As Graphics = Graphics.FromImage(bmp)
        gr.SmoothingMode = SmoothingMode.AntiAlias
        gr.TextRenderingHint = TextRenderingHint.ClearTypeGridFit
        Dim br As New LinearGradientBrush(rect, Color.White, Color.White, LinearGradientMode.Vertical)
        gr.FillRectangle(br, rect)

        Dim strformat As StringFormat = New StringFormat(StringFormatFlags.DirectionVertical)
        strformat.Alignment = StringAlignment.Near
.
.  'draw here
.
        bmp.Save(sI, ImageDefaultFmt)
        bmp.Dispose()
        gr.Dispose()

Open in new window

0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39820485
I don't see anything specific in this method which would cause trouble with multithreading (other than GDI+ blackbox obviously).
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ADO.NET ENTITY DATA MODEL 3 30
Syntax Error 2 44
SSL https .net web site force redirect 3 34
Exit the loop 4 34
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now