473,398 Members | 2,088 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.

How to make a shared class that holds catched data?

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();
}
}
}
//--------------------------------------------------------------------------
--------


Nov 15 '05 #1
2 2087
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 beavailable for all clients by using static method/member.

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

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

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();
}
}
}
//------------------------------------------------------- ---------------------------


.

Nov 15 '05 #2
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" <tr***@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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();
}
}
}
//--------------------------------------------------------------------
------ --------

Nov 15 '05 #3

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

Similar topics

6
by: Paul | last post by:
Hi. Just trying to find out the best approach as I beleive it might give me problems later on down the road. I have an ASP.NET application which references a shared database class which...
12
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. ...
1
by: Trung | last post by:
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....
2
by: tshad | last post by:
I have a program I am trying to compile into a dll and am getting a bunch of: the following errors: error BC30469: Reference to a non-shared member requires an object reference. At first, I...
2
by: John Granade | last post by:
I'm looking for the best way to make a dataset available from multiple Windows forms. The dataset is created from an XML file. I have a main form (frmMain) that loads the dataset and reads the...
8
by: Scott Emick | last post by:
I am using the following to compute distances between two lat/long coordinates for a store locator - (VB .NET 2003) it seems to take a long time to iterate through like 100-150 locations -...
4
by: vze1r2ht | last post by:
I have many types of classes and I'm deciding whether to use a single class or multiple classes for EACH type of class. For an example: User class has 3 classes associated with it: User...
10
by: tshad | last post by:
I have a Dll I created in VS 2000. The namespace is MyFunctions and the Class is CryptoUtil. I have a program that is using the Class but it can't access it directly. I have a class (below)...
3
by: cj2 | last post by:
In the code below I want when this web service is first published for the boolean Running.running to be True. It seems to default to false. How can I make this default to True? Once the web...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
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
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...

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.