By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,890 Members | 1,269 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,890 IT Pros & Developers. It's quick & easy.

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

P: n/a
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));
}
}
}
}
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" <gu***********@yahoo.com> wrote in message
news:14**************************@posting.google.c om...
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));
}
}
}
}

Nov 15 '05 #2

P: n/a
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 [MVP]" <ri********@hotmail.com> wrote in message news:<OK**************@TK2MSFTNGP09.phx.gbl>...
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" <gu***********@yahoo.com> wrote in message
news:14**************************@posting.google.c om...
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));
}
}
}
}

Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.