C
CannonFodder
hi all, im new to c# and i think i may have coded myself into a bit of
a mess.
basically i am writing a windows service which audits all the pcs on a
network using WMI on a regular basis. The main processes are
performed in a Windows Service which scans Active Directory and for
each computer found it adds a process to the threadpool.
the process creates a new instance of a class which does the WMI
querying, this creates an XML file for each successfully scanned
machine.
one of the requirements for this project is to have this service
running, but when the host machine is logged on; to have a little
application which shows the status of the scanner, ie some info about
what machine (if any it is scanning), maybe a progress bar, that sort
of thing.
i know the windows service can't have its own UI, so i set up a remote
object class which has some public variables which are set in the
service, then i have a windows application which reads these public
variables.
this works fine for about a minute and then the application just
hangs.
This is the onStart code of the windows service, it registers the
remote object as a singleton and then creates a local reference to the
singleton to set parameters to it.
--------------------------------------------------------CODE
SAMPLE----------------------------------
protected override void OnStart(string[] args)
{
//register the singleton object on the tcpchannel
TcpChannel chan1 = new TcpChannel(8085);
ChannelServices.RegisterChannel(chan1);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(Singleton.singletonScannerStatus),
"ScannerStatus",
WellKnownObjectMode.Singleton);
Type oType = typeof(Singleton.singletonScannerStatus);
string strUri = "tcp://localhost:8085/ScannerStatus";
m_Obj = (Singleton.singletonScannerStatus)Activator.GetObject(oType,strUri);
.....
....
...
..
---------------------------------------------------------------------------------------------------------------
i've been looking into thread safety and believe this could
potentially be where my app is falling over, and i found references to
'double-checking' within the singleton class, so i added this code the
singleton class:
--------------------------------------------------------CODE
SAMPLE----------------------------------
private static singletonScannerStatus instance = null;
private int m_counter=0;
private string strStatus;
private static readonly object objPadlock = new object();
private singletonScannerStatus()
{
}
public static singletonScannerStatus Instance
{
get
{
if(instance ==null)
{
lock(objPadlock)
{
if(instance == null)
instance = new singletonScannerStatus();
}
}
return instance;
}
}
---------------------------------------------------------------------------------------------------------------
These are the 2 main methods of this class i am trying to use for my
appliaction. The windows service while its looping through the
computers and scanning them it calls "setStatus(string strNewStatus)"
and while this is happening i have a windows application which has a
timer and every second it calls "getStatus()" and puts the result into
a label object on a windows form.
--------------------------------------------------------CODE
SAMPLE----------------------------------
public void setStatus(string strNewStatus)
{
lock(objPadlock)
{
strStatus = strNewStatus;
}
}
public string getStatus()
{
lock(objPadlock)
{
return strStatus;
}
}
---------------------------------------------------------------------------------------------------------------
but the problem still exists, i've been racking my brain all day
trying to figure this one out and now have a head ache, any help is
much appreciated!
thanks in advance
Gavin
a mess.
basically i am writing a windows service which audits all the pcs on a
network using WMI on a regular basis. The main processes are
performed in a Windows Service which scans Active Directory and for
each computer found it adds a process to the threadpool.
the process creates a new instance of a class which does the WMI
querying, this creates an XML file for each successfully scanned
machine.
one of the requirements for this project is to have this service
running, but when the host machine is logged on; to have a little
application which shows the status of the scanner, ie some info about
what machine (if any it is scanning), maybe a progress bar, that sort
of thing.
i know the windows service can't have its own UI, so i set up a remote
object class which has some public variables which are set in the
service, then i have a windows application which reads these public
variables.
this works fine for about a minute and then the application just
hangs.
This is the onStart code of the windows service, it registers the
remote object as a singleton and then creates a local reference to the
singleton to set parameters to it.
--------------------------------------------------------CODE
SAMPLE----------------------------------
protected override void OnStart(string[] args)
{
//register the singleton object on the tcpchannel
TcpChannel chan1 = new TcpChannel(8085);
ChannelServices.RegisterChannel(chan1);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(Singleton.singletonScannerStatus),
"ScannerStatus",
WellKnownObjectMode.Singleton);
Type oType = typeof(Singleton.singletonScannerStatus);
string strUri = "tcp://localhost:8085/ScannerStatus";
m_Obj = (Singleton.singletonScannerStatus)Activator.GetObject(oType,strUri);
.....
....
...
..
---------------------------------------------------------------------------------------------------------------
i've been looking into thread safety and believe this could
potentially be where my app is falling over, and i found references to
'double-checking' within the singleton class, so i added this code the
singleton class:
--------------------------------------------------------CODE
SAMPLE----------------------------------
private static singletonScannerStatus instance = null;
private int m_counter=0;
private string strStatus;
private static readonly object objPadlock = new object();
private singletonScannerStatus()
{
}
public static singletonScannerStatus Instance
{
get
{
if(instance ==null)
{
lock(objPadlock)
{
if(instance == null)
instance = new singletonScannerStatus();
}
}
return instance;
}
}
---------------------------------------------------------------------------------------------------------------
These are the 2 main methods of this class i am trying to use for my
appliaction. The windows service while its looping through the
computers and scanning them it calls "setStatus(string strNewStatus)"
and while this is happening i have a windows application which has a
timer and every second it calls "getStatus()" and puts the result into
a label object on a windows form.
--------------------------------------------------------CODE
SAMPLE----------------------------------
public void setStatus(string strNewStatus)
{
lock(objPadlock)
{
strStatus = strNewStatus;
}
}
public string getStatus()
{
lock(objPadlock)
{
return strStatus;
}
}
---------------------------------------------------------------------------------------------------------------
but the problem still exists, i've been racking my brain all day
trying to figure this one out and now have a head ache, any help is
much appreciated!
thanks in advance
Gavin