473,670 Members | 2,646 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Val ue = Convert.ToInt32 (strInput);
Console.Write(S haredClass.Valu e);
Console.ReadLin e();
}
}
}
//--------------------------------------------------------------------------
--------
//-----------------------------
Client2.cs ---------------------------------------

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

[STAThread]
static void Main(string[] args)
{
Console.WriteLi ne(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.ReadLin e();
}
}
}
//--------------------------------------------------------------------------
--------


Nov 15 '05 #1
2 2094
One way is to create a class which is derived from
MarhalByRefObje ct. you can use .Net remoting to create
the class as a WellKnownSingle Object. 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
InitialLeaseTim e 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.Val ue = Convert.ToInt32 (strInput); Console.Write(S haredClass.Valu e);
Console.ReadLin e();
}
}
}
//------------------------------------------------------- ---------------------------
//-----------------------------
Client2.cs ---------------------------------------

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

[STAThread]
static void Main(string[] args)
{
Console.WriteLi ne(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.ReadLin e();
}
}
}
//------------------------------------------------------- ---------------------------


.

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******** ********@TK2MSF TNGP12.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.Val ue = Convert.ToInt32 (strInput);
Console.Write(S haredClass.Valu e);
Console.ReadLin e();
}
}
}
//--------------------------------------------------------------------
------ --------
//-----------------------------
Client2.cs ---------------------------------------

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

[STAThread]
static void Main(string[] args)
{
Console.WriteLi ne(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.ReadLin e();
}
}
}
//--------------------------------------------------------------------
------ --------

Nov 15 '05 #3

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

Similar topics

6
1855
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 contains methods for serialising and de-serialising objects to the database storage. I put this as a shared class as multiple web clients will be using the class to store and retreive data, the problem I'm haivng now is that I think multiple threads...
12
3293
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. Such things happen. The whole subsystem is going through radical changes. I don't really want to say what I think of the code just yet. That would influence the opinions of others, and I really want to know how other people view these things,...
1
3467
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. 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
2
2953
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 thought it was because I had the sub set as shared, but I get the same error if it take the Shared out. What is causing these errors?
2
6896
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 data but then I have other forms that give the ability to add, modify, and delete rows. This of course changes the dataset and I need that reflected in the main form. At first I was actually passing a reference of the dataset to the "modify" form...
8
3260
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 - about 10-15 seconds...I want to make the code faster. I changed it to be multi-threaded, and it doesn't really make it any faster. The bottleneck seems to be with the math computations. Any ideas like changing my data types or other ideas etc would...
4
2219
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 Class - Holds user properties. Has NO DB related methods (infact has NO methods, only properties lol) etc User Collection class - Holds a collection of user objects
10
2106
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) called CryptoUtil. The functions are Shared functions. I have this Dll in the bin folder of my program. If I don't reference it - it can't seem to find it. If I have:
3
1700
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 service is running and I call it with "start" it works fine as coded. I just don't want to have to call it with start the first time. Imports System.Web.Services Imports System.Web.Services.Protocols Imports System.ComponentModel Imports...
0
8903
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8592
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
8661
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6216
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
5686
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4213
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2802
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
2044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1795
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.