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
390 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39816318
Is the bitmap declared within the method? Are you saving the form to a bitmap?
0
 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
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…

792 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