Class to get a thread safe count of previous Instances

There have been many examples,posted on CodeGuru, of how to check for a previously running instance of an application. Some used DDE, others mutexs but what struck me as the easiest suggestion was to use a 'shared section' to store a flag - accessable by every instance.

The following class uses that technique, along with a couple of the 'interlocked' functions to make it threadsafe. Instead of a simple flag, a count of running instances is kept. The count obtained by the class is only a snapshot.

To use the class simply create a static instance of it. Then check the 'Count()' method to determine how many instances of your application where running before the CPreviousInstance object was instantiated.

Code tested on WinNT 4.0 spk 4 VC6.0 spk 2

// CPreviousInstance.h

class CPreviousInstance  
{
public:
	CPreviousInstance();
	virtual ~CPreviousInstance();

	LONG Count() const
	{
		return m_previous;
	}

private:
	static LONG		s_count;
	LONG			m_previous;

	CPreviousInstance(const CPreviousInstance&);
	CPreviousInstance& operator=(const CPreviousInstance&);
};

// CPreviousInstance.cpp

// static instance count stored in a shared read/write section

#pragma data_seg("Instance")

LONG  CPreviousInstance::s_count = 0;

#pragma data_seg()
#pragma comment(linker,"/section:Instance,rws")


// Construction/Destruction

CPreviousInstance::CPreviousInstance():
m_previous(0)
{
	m_previous = ::InterlockedIncrement(&s_count);
	--m_previous;
}

CPreviousInstance::~CPreviousInstance()
{
	::InterlockedDecrement(&s_count);
}




Comments

  • Even Jeffrey Richter did neglect the crash bug.

    Posted by Legacy on 10/17/2000 12:00am

    Originally posted by: Franz

    I saw Mike Junkin's proposal in a slightly different coding published in Jeffrey Richter's Advanced Windows, an even an expert like him did not point out that this method fails when one of the instances crashes.

    This proofs to me how much source code may exist which holds 'logical' bugs and un-thought possibilities.

    Reply
  • What about a crash

    Posted by Legacy on 03/15/1999 12:00am

    Originally posted by: Mike

    Some of the other methods allow the system to handle a crash. Wouldn't your count be incorrect if one of the instances would take a dive?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As mobile devices have pushed their way into the enterprise, they have brought cloud apps along with them. This app explosion means account passwords are multiplying, which exposes corporate data and leads to help desk calls from frustrated users. This paper will discover how IT can improve user productivity, gain visibility and control over SaaS and mobile apps, and stop password sprawl. Download this white paper to learn: How you can leverage your existing AD to manage app access. Key capabilities to …

  • Live Event Date: September 17, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this upcoming eSeminar and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds