473,398 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

"Object in use" error when multiple objects hear event

tlhintoq
3,525 Expert 2GB
I'm pretty sure this is language independent and is going to be the same whether it's VC or C# - but my project is C# WIndows Forms just in case.

Does anyone have a good handle on the sequence of events when an event is raised and multiple classes are all subscribed to the event?
  • Does each class get a copy of the argument?
  • Does each class get a pointer/address/ref to the one and only argument?
  • Do all the classes get the argument at once, or is passed in subscriber sequence as it falls out of scope?
  • Can the classes each work independently (own thread) with the argument object, or does one have to complete and release the object so it can be passed to the next class in the order they subscribed.?

I have spent hours trying to find the official answer and either Microsoft won't cop to it, or I can't think of the right search terms.

Here's my situation:
I want to pass an arguments object that has a Bitmap, a string and couple other values each time a given event is raised.
Several other classes are subscribed to that event.
So one should process it and save it, one should email it, one should display it and so on. Every class works fine when it is the only class subscribed, but as more classes subscribe to the event the more often I get an "object in use" error randomly.

The kicker is that each class as soon as it gets the Event with argument, makes a low level clone of the argument and works with the clone. So I just can't figure out how each class with its own clone of the arguments can still be affected by another class with its own clone.

Maybe I'm not making a true clone?
Expand|Select|Wrap|Line Numbers
  1.     public class StepArgs : EventArgs, ICloneable
  2.     {
  3.         private string message = string.Empty;
  4.         private List<string> idlist = new List<string>();
  5.         private Image photo = MyCoolProgram.Properties.Resources.TronBit;//Just a placeholder really
  6.         private string filepath = string.Empty;
  7.  
  8.  
  9.         public string Message
  10.         {
  11.             get { return message; }
  12.             set { message = value; }
  13.         }
  14.         public List<string> IDlist = new List<string>();
  15.         public Image Photo
  16.         {
  17.             get { return photo; }
  18.             set { photo = value; }
  19.         }
  20.  
  21.         public string FilePath
  22.         {
  23.             get { return filepath; }
  24.             set { filepath = value; }
  25.         }
  26.  
  27.  
  28.         public override string ToString()
  29.         {
  30.             return "StepArgs";
  31.         }
  32.         public StepArgs()
  33.         {
  34.             IDlist.Add(string.Empty);
  35.             //IDlist.Clear();
  36.             Message = string.Empty;
  37.             FilePath = string.Empty;
  38.             Photo = ThrillCapture.Properties.Resources.Bit;
  39.         }
  40.  
  41.         #region ICloneable Members
  42.  
  43.         public object Clone()
  44.         {
  45.             return (object)this.MemberwiseClone();
  46.         }
  47.  
  48.         #endregion
  49.     }
  50.  
So I'm asking if anyone can straighten me out on my conceptual understanding of the sequence of what happens to the arguments of event, when subscribed to by multiple classes.
Mar 28 '09 #1
1 1921
tlhintoq
3,525 Expert 2GB
So guess what ... I found the object in use, and it wasn't my object or argument.
It was the ImageCodecInfo from the System.Drawing.Imaging namespace - as part of the SaveJPG method.

It would appear that when you get this info you are tied to it until the variable that uses it goes out of scope. So if you have another class that also wants to know the codec info... Well, tough luck Charlie.

Expand|Select|Wrap|Line Numbers
  1.                 ImageCodecInfo jpegCodec = getEncoderInfo("image/jpeg");
  2.  
  3.                 EncoderParameters encoderParams = new EncoderParameters(1);
  4.                 encoderParams.Param[0] = qualityParam;
Notice that when you make the first ImageCodecInfo object (line 1) that you aren't doing it with 'new' specifier. One would assume that you are getting some sort of structure back that is a COPY of the codec's information. But I think I have just proven to myself that what you really get back in a handle to the codec information and no other class can establish a new handle until this one is closed (line 3)

I guess System.Drawing is full of this stuff. Its like that thing were if you make an image from path, you are linked to the path unless you copy it into a new image and dump the link one. Thanks M$.

Trial, lots of error, and following step by step with the debugger. Damn I wish I would follow my own advice more often!
Mar 29 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: J. Muenchbourg | last post by:
I have a label object that gets a dynamic value thrown into it on a button submit event: lblMessage.InnerText = txtName.Value but i get "Object reference not set to an instance of an object"...
11
by: Florian Loitsch | last post by:
I'm currently writing a JS->Scheme compiler (which, using Bigloo, automatically yields a JS->C, JS->JVM, JS->.NET compiler), and have a question concerning the function-parameters: According to...
7
by: deko | last post by:
I'm getting intermittent "Object Invalid or No Longer Set" errors in my Access 2002 mdb. What causes these errors? Has anyone dealt with this before? I can't trace it because it's not easy...
3
by: Karel Vandenhove | last post by:
Hi, I get an error "cannot apply indexing with to an expression of type object" when I try to compile the code below. SSLScannerManager is a COM component. (Used to access fingerprint...
1
by: Lauchlan M | last post by:
Hi. I'm using ASP.NET, getting an "Object reference not set to an instance of an object" error. In my login.aspx page I have: string arrUserRoles = new string {"UserRole"};...
5
by: Christian Hvid | last post by:
What is the easiest way to get the "row object" or "item object" when a datagrid is clicked? I have web form with a datagrid. And I have an array of something called BlogEntry that I bind to the...
2
by: chuckdfoster | last post by:
I am getting a "Could Not Access CDO.Message Object" Error when I try to use the following code to send an email via ASP.NET. When I run this on one machine it works, on another one it doesn't. ...
2
by: louie.hutzel | last post by:
This JUST started happening, I don't remember changing any code: When I click the submit button on my form, stuff is supposed to happen (which it does correctly) and a result message is posted back...
3
by: =?Utf-8?B?QmFkaXM=?= | last post by:
I'm doing a server side automation(I know it's bad but..) and its working fine locally and when accessing it from a remote machine using web browser is was giving me this error"Retrieving the COM...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.