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

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

lep1Asked:
Who is Participating?
 
CodeCruiserConnect With a Mentor Commented:
I don't see anything specific in this method which would cause trouble with multithreading (other than GDI+ blackbox obviously).
0
 
CodeCruiserCommented:
Is the method accessing any variables within the class? Or is it calling any other methods which may be accessing variables?
0
 
lep1Author Commented:
Yes, but the exception occurred during a bmp.save after creating a graphic bitmap.

The error was an unhandled exception.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
lep1Author Commented:
Should I check that New is used for creating the bitmap and the graphic?
0
 
CodeCruiserCommented:
Is the bitmap declared within the method? Are you saving the form to a bitmap?
0
 
lep1Author Commented:
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
 
lep1Author Commented:
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
 
CodeCruiserCommented:
Can you show your class code?
0
 
lep1Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.