473,765 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

accessing a class instance from a separate class running in a separate thread

I have a class with data and methods that use it. Everything is
contained perfectly

THE PROBLEM: A separate thread has to call a method in the current
instantiation of this class. There is only ever ONE instantiation of
this class, and this outside method in a separate thread has to access
it.

How do i do this?

POSSIBLE SOLUTION: Have a static pointer to the class that is updated
to point to the current instantiation. The downside to this, is I
must the outside thread is not calling the method when the class is
not instantiated.

POSSIBLE SOLUTION #2: Make it a static class, as there's only ever
one instance of it at any given point in time. The downside to this,
is the data should be cleared away for when each new instance was
created, and instead of worrying about resetting old data, I made the
class require instantiation so each instance has its own fresh data.

What do you think?

Apr 9 '07
12 3119
OD <OD <webmaster @ e-naxos dot com>wrote:
Unfortunately the latter page doesn't give a thread-safe version,

that's true, but why speaking about the second and not the first that
is showing a thread safe implementation. ..
Because I wrote the first one and have no problems with it :)

All I'm saying is that the second article isn't a particularly good
one, but that may well not be obvious to a reader.
It was, as I said it, 'examples'. Just to show a few ways to implement
the pattern.
There is not a 'legal' and 'unique' way to implement each design
pattern, each developer has his/her own way, and certainly, the
Singleton is the one with the most numerous versions..
Yes, and a lot of them aren't thread-safe - why bother referring to
flawed examples?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 10 '07 #11
OD,

Here's a simplified example of what I am trying to do:

I am analysing data coming to me from an Internet source. I have to
read this data in and analyze it. The data I create from the analysis
should only for the time period I am analyzing the data. I eventually
stop (stops are not predefined, they essentially happen randomly) and
move onto the next time period. I have to make sure I clean out the
data from previous time period when I move on to the next.

Instead of cleaning out old data from a static class, I thought why
not use instances? Then, each time period has its own data. No
confusion.

The problem... however... is the incoming data is from another thread,
which must access this class to input the data into it. During switch-
over from one instance to the next (whether it is a singleton class or
not), the data might be pointing to the wrong instance (or might be
pointing to null)!

I solved this by creating a global reference to the current instance.
The class constructor sets this global reference to point to itself,
and it points it to null when it quits. Then, I made the incoming
data make sure it is not null by handling a NullReferenceEx ception, as
it should never happen at all (if you really knew what my program was
doing you would see this).

I still made this outside thread handle a NullReferenceEx ception,
because it is dependant on a variable to point to a class that it has
no control over. It would be unfair (and not robust) to expect it to
depend on such a resource.

I think I have my solution... I think this works. To recap: I am
using a static (global) reference to a class instance, which an
outside thread uses to access the class instance. The static
reference is updated by each instance of the class.

It would screw up if two instances were created, because they would
both change this static reference to point to themselves, but that
should never happen. But, just to be robust, maybe I should do that
singleton thing to catch for such errors.

Titan

Apr 10 '07 #12
In other words, what I have now works.

But, I should code a catch for mistaken double instances (which are
not supposed to happen). And to do that, I use singletons, right?

Titan

Apr 10 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1561
by: Mike Frayn | last post by:
Hello there. I'm kinda new to Java, did some last year and have forgotten it all, but am experienced enough with C++. I have a problem here that I would imagine is solved very easily. I have a class that contains a main function: // pseudo-code: please don't condemn the syntax or // missing keywords
17
2441
by: Andrae Muys | last post by:
Found myself needing serialised access to a shared generator from multiple threads. Came up with the following def serialise(gen): lock = threading.Lock() while 1: lock.acquire() try: next = gen.next() finally:
7
1653
by: Doug Thews | last post by:
I'm writing some sample threading code, and have already got many examples working that look a lot similar to the ones mentioned here. What I'm seeing is that most people put their method that will be run as a thread inside the overall Form's class, which is OK for examples, but not really what you'd see in real life. So, I decided to write a class (called BusinessOperation) and have properties & methods in it. Then, I have 1 method in...
3
3345
by: AdamM | last post by:
Hi all, When I run my VbScript, I get the error: "ActiveX component can't create object: 'getobject'. Error 800A01AD". Any ideas what I did wrong? Here's my VBScript: dim o set o=getobject(,"ConsoleApplication2.Program") msgbox o.TestString
6
1914
by: Vladislav Kosev | last post by:
I have this strange problem now twice: I am writing this relatevely large web site on 2.0 and I made a static class, which I use for url encoding and deconding (for remapping purposes). This static class needs the session context to encode a url (because I stored the current language there), so I made a static field of type HttpContext, which I refresh every reqest by assigning the current context. Now, every now and then I get this...
6
2688
by: MattPKaiser | last post by:
I am trying to find a way to check if a computer on a network is "online" so that I can access a file on a share. In short I maintain a list of computers that have my service running (in the client application) and in my client program I allow the user to change the computer that they are accessing. If a computer that is on the list becomes unplugged and I try to access a file with a FileStream the filestream creation takes 8-10...
4
2140
by: Lilith | last post by:
This was easy in C++ (well, relatively.) Now I'm in the C# environment and everything I try doesn't work. I'm running a thread in which I have two accesses I need to components on a form. In one instance it's to simply check the text of a button as an indicator of whether I've got a request to end the thread. This is a read-only situation so I don't have to worry about synchronization. The other is a need to write status information...
1
5059
by: Tobias Maier | last post by:
Hi I want to start a C# ActiveX Exe with a parameter. Now, start a WebBrowser. Within Html/javascript I initiate my component und want to retrieve the parameter from the startet exe. I have done this in VB6 with new ActiveXObject("name") and all works correct. Using C# or Vb.net it works different:
1
4801
by: schnandr | last post by:
Hi, I have a user control which contains ListView. I copy all ListViewItems into a separate List<ListViewItemcalled unfilteredItems. I am using a BackgroundWorker to filter the ListView. When I access this list in the DoWork Handler I am getting an InvalidOperationException that the List was not created by this thread. Is there a way to solve this problem?
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8833
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.