G
Gulshan Oshan
I want to implement a simple console that continuously listens for an
event from a custom object. I am unable to capture the events from
the object.
If I subscribe to the events in a windows app it works fine. Any
idea?
using System;
using MyTestApp.Messaging;
using MyTestApp.BusinessLayer;
using System.Threading;
namespace MyTestApp.Listener
{
/// <summary>
/// MyTestApp.Listener.Run
/// </summary>
class Run
{
/// <summary>
/// Constructor
/// </summary>
public Run()
{
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
try
{
Background background = new Background();
Thread th = new Thread(new ThreadStart(background.SpinInfinite));
th.Priority = ThreadPriority.Lowest;
th.Start();
Run run = new Run();
ObjectSubscriber _ObjSubs = new ObjectSubscriber("_LOCAL.Test");
_ObjSubs.ObjectReceived += new ObjectReceivedEvent(run.DoProcess);
Console.WriteLine("Listening");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
/// <summary>
/// This is the event handler
/// </summary>
/// <param name="objTest">event args</param>
public void DoProcess(object objTest)
{
TestObject obj = (TestObject)objTest;
Console.WriteLine("Object Received : ");
Console.WriteLine("N1="+ obj.n1);
Console.WriteLine("N2="+ obj.n2);
Console.WriteLine("STR="+ obj.str);
}
}
}
//----- BACKGROUND.CS---------------------
namespace MyTestApp.TestListener
{
/// <summary>
/// Background class keeps the process alive.
/// </summary>
public class Background
{
public Background()
{
}
public bool Terminate = false;
public void SpinInfinite()
{
while (!Terminate)
{
Thread.Sleep(new TimeSpan(0,0,3));
}
}
}
}
event from a custom object. I am unable to capture the events from
the object.
If I subscribe to the events in a windows app it works fine. Any
idea?
using System;
using MyTestApp.Messaging;
using MyTestApp.BusinessLayer;
using System.Threading;
namespace MyTestApp.Listener
{
/// <summary>
/// MyTestApp.Listener.Run
/// </summary>
class Run
{
/// <summary>
/// Constructor
/// </summary>
public Run()
{
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
try
{
Background background = new Background();
Thread th = new Thread(new ThreadStart(background.SpinInfinite));
th.Priority = ThreadPriority.Lowest;
th.Start();
Run run = new Run();
ObjectSubscriber _ObjSubs = new ObjectSubscriber("_LOCAL.Test");
_ObjSubs.ObjectReceived += new ObjectReceivedEvent(run.DoProcess);
Console.WriteLine("Listening");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
/// <summary>
/// This is the event handler
/// </summary>
/// <param name="objTest">event args</param>
public void DoProcess(object objTest)
{
TestObject obj = (TestObject)objTest;
Console.WriteLine("Object Received : ");
Console.WriteLine("N1="+ obj.n1);
Console.WriteLine("N2="+ obj.n2);
Console.WriteLine("STR="+ obj.str);
}
}
}
//----- BACKGROUND.CS---------------------
namespace MyTestApp.TestListener
{
/// <summary>
/// Background class keeps the process alive.
/// </summary>
public class Background
{
public Background()
{
}
public bool Terminate = false;
public void SpinInfinite()
{
while (!Terminate)
{
Thread.Sleep(new TimeSpan(0,0,3));
}
}
}
}