D
djdouma
I've been working on this one for a couple of days, and am completly
at my wits end. Is there some method for executing a function or
event in the main thread, when called from a 'worker' thread? I
understand how it works in the Windows forms world, but what about an
application with no GUI?
I provided example code and the output below. What I'm looking for is
to have the prog_ShowPopUpEvent method executed by the main thread.
Thanks!
Output:
MAIN THREAD : Sleeping for 1 second
MAIN THREAD : Sleeping for 1 second
MAIN THREAD : Sleeping for 1 second
MAIN THREAD : Sleeping for 1 second
MAIN THREAD : Sleeping for 1 second
TIMER THREAD : timer elapsed
TIMER THREAD : fire show pop up event
TIMER THREAD : show pop up event fired
Main thread exiting.
Code:
using System;
using System.Threading;
namespace ConsoleApplication1
{
public class Program
{
private bool _running = false;
private event EventHandler ShowPopUpEvent;
[STAThread]
public static void Main()
{
Program prog = new Program();
prog.Run();
}
private void Run()
{
this.ShowPopUpEvent += new
EventHandler(prog_ShowPopUpEvent);
Thread.CurrentThread.Name = "MAIN THREAD";
System.Timers.Timer checksTimer = new
System.Timers.Timer();
//Elapse every 5 seconds
checksTimer.Interval = 5000;
checksTimer.Elapsed +=new
System.Timers.ElapsedEventHandler(checksTimer_Elapsed);
checksTimer.Enabled = true;
this._running = true;
do
{
Console.WriteLine("{0} : Sleeping for 1 second",
Thread.CurrentThread.Name);
Thread.Sleep(1000);
}
while (this._running);
Console.WriteLine("Main thread exiting.");
}
private void prog_ShowPopUpEvent(object sender, EventArgs e)
{
Console.WriteLine("{0} : show pop up event fired",
Thread.CurrentThread.Name);
this._running = false;
}
void checksTimer_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
if (Thread.CurrentThread.Name == null)
Thread.CurrentThread.Name = "TIMER THREAD";
Console.WriteLine("{0} : timer elapsed",
Thread.CurrentThread.Name);
//Perform a bunch of checks
if (true == false)
{
}
else
{
//Show the popup window
OnShowPopup();
}
}
private void OnShowPopup()
{
if (ShowPopUpEvent != null)
{
Console.WriteLine("{0} : fire show pop up event",
Thread.CurrentThread.Name);
ShowPopUpEvent(this, new EventArgs());
}
}
}
}
at my wits end. Is there some method for executing a function or
event in the main thread, when called from a 'worker' thread? I
understand how it works in the Windows forms world, but what about an
application with no GUI?
I provided example code and the output below. What I'm looking for is
to have the prog_ShowPopUpEvent method executed by the main thread.
Thanks!
Output:
MAIN THREAD : Sleeping for 1 second
MAIN THREAD : Sleeping for 1 second
MAIN THREAD : Sleeping for 1 second
MAIN THREAD : Sleeping for 1 second
MAIN THREAD : Sleeping for 1 second
TIMER THREAD : timer elapsed
TIMER THREAD : fire show pop up event
TIMER THREAD : show pop up event fired
Main thread exiting.
Code:
using System;
using System.Threading;
namespace ConsoleApplication1
{
public class Program
{
private bool _running = false;
private event EventHandler ShowPopUpEvent;
[STAThread]
public static void Main()
{
Program prog = new Program();
prog.Run();
}
private void Run()
{
this.ShowPopUpEvent += new
EventHandler(prog_ShowPopUpEvent);
Thread.CurrentThread.Name = "MAIN THREAD";
System.Timers.Timer checksTimer = new
System.Timers.Timer();
//Elapse every 5 seconds
checksTimer.Interval = 5000;
checksTimer.Elapsed +=new
System.Timers.ElapsedEventHandler(checksTimer_Elapsed);
checksTimer.Enabled = true;
this._running = true;
do
{
Console.WriteLine("{0} : Sleeping for 1 second",
Thread.CurrentThread.Name);
Thread.Sleep(1000);
}
while (this._running);
Console.WriteLine("Main thread exiting.");
}
private void prog_ShowPopUpEvent(object sender, EventArgs e)
{
Console.WriteLine("{0} : show pop up event fired",
Thread.CurrentThread.Name);
this._running = false;
}
void checksTimer_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
if (Thread.CurrentThread.Name == null)
Thread.CurrentThread.Name = "TIMER THREAD";
Console.WriteLine("{0} : timer elapsed",
Thread.CurrentThread.Name);
//Perform a bunch of checks
if (true == false)
{
}
else
{
//Show the popup window
OnShowPopup();
}
}
private void OnShowPopup()
{
if (ShowPopUpEvent != null)
{
Console.WriteLine("{0} : fire show pop up event",
Thread.CurrentThread.Name);
ShowPopUpEvent(this, new EventArgs());
}
}
}
}