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
382 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
Comment Utility
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
Comment Utility
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
Comment Utility
Should I check that New is used for creating the bitmap and the graphic?
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
Is the bitmap declared within the method? Are you saving the form to a bitmap?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:lep1
Comment Utility
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
Comment Utility
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
Comment Utility
Can you show your class code?
0
 

Author Comment

by:lep1
Comment Utility
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
Comment Utility
I don't see anything specific in this method which would cause trouble with multithreading (other than GDI+ blackbox obviously).
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This video discusses moving either the default database or any database to a new volume.

772 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

11 Experts available now in Live!

Get 1:1 Help Now