Wait indefinately for events in C# console app (Threading)

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

Rick Strahl [MVP]

It looks like your main thread is just exiting and the object goes with it.
You need something to keep the main thread alive so that the object is
there.

I'm not sure I understand what this code is supposed to do. It looks like
this code should fire the DoProcess() method exactly once.

I think what you want to do is:

Create a new object.
Create a new thread.
From the thread hook up the event handler on the new object (which was
created on another thread!)
Fire whenever the time comes

Now your event handler should fire but from the worker thread.

How you hook up to the event is going to be important. Usually it's best to
wrap the whole threaded piece into an object so all state goes with the
thread.

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/wwHelp
 
G

Gulshan Oshan

Thanks Rick,

It worked! I created a worker thread like you described and
instantiated and subscribed to events in it. I kept background thread
to keep the console app alive. I dont understand why I couldnt
subscribe to events in the Main method of the console app. Anything
to do with STAThread?

Some background in case you are wondering what I am trying to get
done. What I had been trying to do was listen to Tibco messages from
this console app. It was a sort of test for me to get a feel for
Tibco and to consider using it as a more reliable and simpler way of
distributed computing. I serialize objects and send them over the
network via Tibco as opaque bytes to load balanced queues (Tibco
Distributed Queues). On the receiving end I get appropriate events,
deserialize object and work with it.

Thanks again
Gulshan S. Oshan




Rick Strahl said:
It looks like your main thread is just exiting and the object goes with it.
You need something to keep the main thread alive so that the object is
there.

I'm not sure I understand what this code is supposed to do. It looks like
this code should fire the DoProcess() method exactly once.

I think what you want to do is:

Create a new object.
Create a new thread.
From the thread hook up the event handler on the new object (which was
created on another thread!)
Fire whenever the time comes

Now your event handler should fire but from the worker thread.

How you hook up to the event is going to be important. Usually it's best to
wrap the whole threaded piece into an object so all state goes with the
thread.

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/wwHelp
----------------------------------
Making waves on the Web


Gulshan Oshan said:
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));
}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top