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
392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
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

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 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