all groups > dotnet remoting > april 2007 >
You're in the

dotnet remoting

group:

Windows Service static object method accessible trough remoting


Windows Service static object method accessible trough remoting Faessler Gilles
4/3/2007 6:26:00 AM
dotnet remoting: Hello,

I currently have a windows service which handles multiple threads and a
shared queue object in which I had jobs to process.

Now I would like to be able to add jobs on the windows service queue trough
a .net remoting call.

I had a look at .net remoting documentation but i'm not experienced with it
so I'm not sure how to achieve what I want. Below is a shortened sample of my
code.

Should i make a method in program which returns my static jobQueue to call
AddJob on it or maybe I can build a delegate to call jobQueue.AddJob()
whithout exposing the class ?

Any help is welcome

Kind regards

Gilles

Here is a sample of the windows service code :
- The program class is the main class of my service
- The jobQueue is a static instance of InputOutputProducer and is used among
different threads to manage the queue trough the AddJob() and DequeueJob()
methods which are threadsafe.
- Each thread runs individually, builds jobs and then add them to the Queue
trough Program.jobQueue.AddJob() ;

- What I want to do is to be able to call the AddJob method of my existing
jobQueue from a website using .NET Remoting. I think I need to make a
singleton SAO but i'm a bit stucked to build

public class Program
{
public static InputOutputProducer jobQueue;

public static void Start()
{
jobQueue = new InputOutputProducer();

new Thread(new ThreadStart(EnqueueFTP)).Start();
new Thread(new ThreadStart(EnqueueMailPuller)).Start();


while (true)
{
// Dequeue Job
Job job = (Job)jobQueue.DequeueJob();

// Process Job
ProcessJob(job) ;
}
}

/// <summary>
/// Starts FTPWatcher Thread
/// </summary>
public static void EnqueueFTP()
{
FTPWatcher ftpWatcher = new FTPWatcher();
}

/// <summary>
/// Starts MailWatcherThread
/// </summary>
public static void EnqueueMailPuller()
{
MailPuller pullMail = new MailPuller();
}
}

public class InputOutputProducer
{
readonly object listLock = new Object();
Queue queue = new Queue();

public void AddJob(Job job, Enums.Threads currentThread)
{
// Lock Queue while Enqueuing
lock (listLock)
{
queue.Enqueue(job);
Monitor.Pulse(listLock);
}
}

public object DequeueJob()
{
// Lock Queue while Dequeuing
lock (listLock)
{
// If the queue is empty, wait for an item to be added
// Note that this is a while loop, as we may be pulsed but
not wake up before another thread
// has come in and consumed the newly added object. In that
case, we'll have to wait for another pulse.
while (queue.Count == 0)
{
// Release listLock until woken up by Pulse
Monitor.Wait(listLock);
}
return queue.Dequeue();
}
}
}
RE: Windows Service static object method accessible trough remoting Faessler Gilles
4/3/2007 6:38:09 AM
Well I think i found it out. Sometimes writing down what I want to do helps
me to search in the right direction.

My real question was how to expose my windows service object and use it on
the host itself.

By registering my singleton as follow it should solve this issue :

jobQueue = new InputOutputProducer();
HttpChannel canal = new HttpChannel(65100);
ChannelServices.RegisterChannel(canal, false);
RemotingServices.Marshal(jobQueue, "AddObject", typeof(InputOutputProducer));

[quoted text, click to view]
AddThis Social Bookmark Button