HighTechTalks DotNet Forums  

Windows Service static object method accessible trough remoting

Dotnet Framework (Remoting) microsoft.public.dotnet.framework.remoting


Discuss Windows Service static object method accessible trough remoting in the Dotnet Framework (Remoting) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Faessler Gilles
 
Posts: n/a

Default Windows Service static object method accessible trough remoting - 04-03-2007 , 09:26 AM






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

Reply With Quote
  #2  
Old   
Faessler Gilles
 
Posts: n/a

Default RE: Windows Service static object method accessible trough remoting - 04-03-2007 , 09:38 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));

"Faessler Gilles" wrote:

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

Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.