How to make a shared class that holds catched data?

T

Trung

Hi everybody

I write a class named SharedClass that holds catched data that would be
available for all clients by using static method/member.

This class is registered in the assembly folder. And I write 2 client
applications Client1 and Client2 that use SharedClass. But Client2 cannot
get the value that given to SharedClass by Client1.

How can I make SharedClass' data available for all clients? Source code is
attached.

Thanks

Trung

//-----------------------------
SharedClass.cs ---------------------------------
using System;
namespace SharedClasses
{
public class SharedClass
{
private static int intSharedValue = 0;
public SharedClass(){}
public static int Value
{
get
{
return intSharedValue;
}
set
{
intSharedValue = value;
}
}
}
}
//--------------------------------------------------------------------------
-------
//-----------------------------
Client1.cs ----------------------------------------
using System;
using SharedClasses;
namespace Client1
{
public class Client1
{
public Client1(){}

[STAThread]
static void Main(string[] args)
{
string strInput = "100";
SharedClass.Value = Convert.ToInt32(strInput);
Console.Write(SharedClass.Value);
Console.ReadLine();
}
}
}
//--------------------------------------------------------------------------
--------
//-----------------------------
Client2.cs ---------------------------------------

using System;
using SharedClasses;
namespace Client2
{
class Client2
{

[STAThread]
static void Main(string[] args)
{
Console.WriteLine(SharedClass.Value);
//I expected the output is 100, while Client1 is still running.
But it is not. Why?
//I want to get the value given by Client1. How can I do that?
Console.ReadLine();
}
}
}
//--------------------------------------------------------------------------
--------
 
G

Gert-Jan Messchendorp

One way is to create a class which is derived from
MarhalByRefObject. you can use .Net remoting to create
the class as a WellKnownSingleObject. You need to
configure remoting to get this work (see msdn library).
Be aware that the well known single objects have a lease
time, you must do something to keep the object alive (fE
define an infinite leasetime bij overriding the
InitialLeaseTime method and retruning null)

Another way is using platform invocation to make a memory
mapped file, this file can be accessed everywhere.

Gert-Jan
-----Original Message-----
Hi everybody

I write a class named SharedClass that holds catched data that would be
available for all clients by using static method/member.

This class is registered in the assembly folder. And I write 2 client
applications Client1 and Client2 that use SharedClass. But Client2 cannot
get the value that given to SharedClass by Client1.

How can I make SharedClass' data available for all clients? Source code is
attached.

Thanks

Trung

//-----------------------------
SharedClass.cs ---------------------------------
using System;
namespace SharedClasses
{
public class SharedClass
{
private static int intSharedValue = 0;
public SharedClass(){}
public static int Value
{
get
{
return intSharedValue;
}
set
{
intSharedValue = value;
}
}
}
}
//------------------------------------------------------- -------------------
-------
//-----------------------------
Client1.cs ----------------------------------------
using System;
using SharedClasses;
namespace Client1
{
public class Client1
{
public Client1(){}

[STAThread]
static void Main(string[] args)
{
string strInput = "100";
SharedClass.Value = Convert.ToInt32 (strInput);
Console.Write(SharedClass.Value);
Console.ReadLine();
}
}
}
//-------------------------------------------------------
-------------------
--------
//-----------------------------
Client2.cs ---------------------------------------

using System;
using SharedClasses;
namespace Client2
{
class Client2
{

[STAThread]
static void Main(string[] args)
{
Console.WriteLine(SharedClass.Value);
//I expected the output is 100, while Client1 is still running.
But it is not. Why?
//I want to get the value given by Client1. How can I do that?
Console.ReadLine();
}
}
}
//------------------------------------------------------- -------------------
--------




.
 
M

Michael Mayer

The simple answer is that Client1 and Client2 are each applications
and are therefore each loaded into a separate AppDomain (search for
that term on MSDN for more details). When a client loads the
SharedClass, it loads the Assembly into its own AppDomain. The static
variables are only static within an AppDomain, not between the two
separate AppDomains. So you have Client1 running in AppDomain1 with
an instance of SharedClasss. Meanwhile, Client2 is running in
AppDomain2 which is completely separate from AppDomain1.

The easiest solution would be for SharedClasses to use a file /
database / registry to store values between the two. The best
solution is to look at the Remoting classes for communication between
AppDomains.

mike

Trung said:
Hi everybody

I write a class named SharedClass that holds catched data that would be
available for all clients by using static method/member.

This class is registered in the assembly folder. And I write 2 client
applications Client1 and Client2 that use SharedClass. But Client2 cannot
get the value that given to SharedClass by Client1.

How can I make SharedClass' data available for all clients? Source code is
attached.

Thanks

Trung

//-----------------------------
SharedClass.cs ---------------------------------
using System;
namespace SharedClasses
{
public class SharedClass
{
private static int intSharedValue = 0;
public SharedClass(){}
public static int Value
{
get
{
return intSharedValue;
}
set
{
intSharedValue = value;
}
}
}
}
//--------------------------------------------------------------------
------
-------
//-----------------------------
Client1.cs ----------------------------------------
using System;
using SharedClasses;
namespace Client1
{
public class Client1
{
public Client1(){}

[STAThread]
static void Main(string[] args)
{
string strInput = "100";
SharedClass.Value = Convert.ToInt32(strInput);
Console.Write(SharedClass.Value);
Console.ReadLine();
}
}
}
//--------------------------------------------------------------------
------
--------
//-----------------------------
Client2.cs ---------------------------------------

using System;
using SharedClasses;
namespace Client2
{
class Client2
{

[STAThread]
static void Main(string[] args)
{
Console.WriteLine(SharedClass.Value);
//I expected the output is 100, while Client1 is still running.
But it is not. Why?
//I want to get the value given by Client1. How can I do that?
Console.ReadLine();
}
}
}
//--------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top