Windows Service

I'm writing a windows service application and I need many Windows Application to read/write the same variable in windows service. If a client set this variable to a value, I need that a second client read the value set by the first one.

I've tried to declare a static variable, but it don't work.

Sample (in the Windows Service):

....
....
public static int myVar = 0;
public void Add()
{
   myVar++;
}

in the Windows Application:

private void myMethod()
{
   myServiceClass s = new myServiceClass();
   s.Add();   
}


If a first client call the myMethod the variable myVar in the Service will be set to 1.
If a second client call the same method the variable will be set to 2, but this doesn't work, and the variable is always set to 1.

Thanks


Answer this question

Windows Service

  • redocdlab

    Hi,
    I'm writing an application like the Windows Scheduled Tasks. The Windows Service that I wrote will be installed on a server. Many PC client are able to "submit" tasks to the server. If I don't have a database to store info about the tasks, but I have only same class, how can I share the information about this class

    If a first client submit a tasks, how can I share this class with a second, third, etc. clients (without pass from a db)

    Thanks

  • klahking

    Hi,

    You can make use of an XML Configuration file which you read and write to. Alternatively, you could save some specific values in the Windows Registry as well.

    It depends on how much information you have that you want to store. If it is quite a bit, then you can actually save it in a DataSet [u dont need a database, just a DataSet] and then serialize the dataset to file. Then u can read the File again later and regenerate the DataSet also.

    Regards,
    Vikram

  • ramil.d

    Hi,

    Your understanding of the scope of a static variable is incorrect. The scope of the static variable is the application or process in which it exists.

    You cannot refer to that static variable from the Windows Application simply by just referencing the same class since the processes are different and hence the two variables are different.

    You could use .NET Remoting to pass the values and then the value can be set. Or, you could use other techniques such as having the setting in a database or file.

    Regards,
    Vikram

  • Windows Service