F
Franco, Gustavo
Hi, I have a question, and please I need a answer.
How can I finalize a thread running with Application.Run (I need the message
loop!!!) without call Thread.Abort?.
I want to call Application.ExitThread in the same thread that it is running.
So, This is my example and I don't know why WaitProc methods runs on
different thread.
I'm sending the source code, it is really a very simple source code.
1) Create a Form
2) OnFormLoad Create a Thread Background
3) OnFormClosing Destroy my Thread Background (Failing: WaitProc doesn't run
on his own thread)
4) Close Form.
I put Console.Write Lines to identify the threads.
And I got this.
Current Thread Load : 7
Current Thread EntryPoint : 8
Current Thread Constructor() : 8
Current Thread Closing : 7
Current Thread Unload() : 7
Current Thread WaitProc() : 10 <====== Here Why is 10, it should be 8, if I
call Application.ExitThread on thread 10 I can't exit from thread 8.
So, this application hangs.
Please take a look of the example, it is very simple don't close the mail
yet, I don't know what else to do to for execute a method on his own thread,
because that's everything I need. How do I execute a method on his own
thread (without inherit from Form and user Invoke.Required, Invoke)
Thanks so much,
Gustavo.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace Test
{
public class BackgroundClass
{
private AutoResetEvent checkEvent = new AutoResetEvent(false);
public BackgroundClass()
{
Console.WriteLine("Current Thread Constructor() : " +
Thread.CurrentThread.GetHashCode()); // Returned 8
ThreadPool.RegisterWaitForSingleObject(checkEvent, new
WaitOrTimerCallback(WaitProc), null, -1, true);
}
private void WaitProc(object state, bool timedOut)
{
Console.WriteLine("Current Thread WaitProc() : " +
Thread.CurrentThread.GetHashCode()); // Returned 10 "WHY."
Application.ExitThread();
}
public void UnloadClass()
{
checkEvent.Set();
Console.WriteLine("Current Thread Unload() : " +
Thread.CurrentThread.GetHashCode()); // Returned 7
}
}
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private static BackgroundClass background = null;
private Thread newThread = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Closing += new CancelEventHandler(Form1_Closing);
this.Load += new EventHandler(Form1_Load);
this.Text = "Form1";
}
#endregion
private static void ThreadEntryPoint()
{
Console.WriteLine("Current Thread EntryPoint : " +
Thread.CurrentThread.GetHashCode()); // Returned 8
background = new BackgroundClass();
Application.Run();
}
private void Form1_Load(object sender, EventArgs e)
{
Console.WriteLine("Current Thread Load : " +
Thread.CurrentThread.GetHashCode()); // Returned 7
newThread = new Thread(new ThreadStart(ThreadEntryPoint));
newThread.Start();
}
private void Form1_Closing(object sender, CancelEventArgs e)
{
Console.WriteLine("Current Thread Closing : " +
Thread.CurrentThread.GetHashCode()); // Returned 7
background.UnloadClass();
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
How can I finalize a thread running with Application.Run (I need the message
loop!!!) without call Thread.Abort?.
I want to call Application.ExitThread in the same thread that it is running.
So, This is my example and I don't know why WaitProc methods runs on
different thread.
I'm sending the source code, it is really a very simple source code.
1) Create a Form
2) OnFormLoad Create a Thread Background
3) OnFormClosing Destroy my Thread Background (Failing: WaitProc doesn't run
on his own thread)
4) Close Form.
I put Console.Write Lines to identify the threads.
And I got this.
Current Thread Load : 7
Current Thread EntryPoint : 8
Current Thread Constructor() : 8
Current Thread Closing : 7
Current Thread Unload() : 7
Current Thread WaitProc() : 10 <====== Here Why is 10, it should be 8, if I
call Application.ExitThread on thread 10 I can't exit from thread 8.
So, this application hangs.
Please take a look of the example, it is very simple don't close the mail
yet, I don't know what else to do to for execute a method on his own thread,
because that's everything I need. How do I execute a method on his own
thread (without inherit from Form and user Invoke.Required, Invoke)
Thanks so much,
Gustavo.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace Test
{
public class BackgroundClass
{
private AutoResetEvent checkEvent = new AutoResetEvent(false);
public BackgroundClass()
{
Console.WriteLine("Current Thread Constructor() : " +
Thread.CurrentThread.GetHashCode()); // Returned 8
ThreadPool.RegisterWaitForSingleObject(checkEvent, new
WaitOrTimerCallback(WaitProc), null, -1, true);
}
private void WaitProc(object state, bool timedOut)
{
Console.WriteLine("Current Thread WaitProc() : " +
Thread.CurrentThread.GetHashCode()); // Returned 10 "WHY."
Application.ExitThread();
}
public void UnloadClass()
{
checkEvent.Set();
Console.WriteLine("Current Thread Unload() : " +
Thread.CurrentThread.GetHashCode()); // Returned 7
}
}
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private static BackgroundClass background = null;
private Thread newThread = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Closing += new CancelEventHandler(Form1_Closing);
this.Load += new EventHandler(Form1_Load);
this.Text = "Form1";
}
#endregion
private static void ThreadEntryPoint()
{
Console.WriteLine("Current Thread EntryPoint : " +
Thread.CurrentThread.GetHashCode()); // Returned 8
background = new BackgroundClass();
Application.Run();
}
private void Form1_Load(object sender, EventArgs e)
{
Console.WriteLine("Current Thread Load : " +
Thread.CurrentThread.GetHashCode()); // Returned 7
newThread = new Thread(new ThreadStart(ThreadEntryPoint));
newThread.Start();
}
private void Form1_Closing(object sender, CancelEventArgs e)
{
Console.WriteLine("Current Thread Closing : " +
Thread.CurrentThread.GetHashCode()); // Returned 7
background.UnloadClass();
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}