473,395 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Please Tell me what Am I doing wrong... Please

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

}
}


Nov 15 '05 #1
5 2385
100
Hi Franco,

The problem is that your WaitProc is executed in the worker thread from the
thread pool (not in the thread you create in the Form1_Load that runs the
message loop).
Application.ExitThread exits the message loop in the current thread. When
called form the worker thread (from the thread pool) there is no message
loop to exit from.

My question is why do you need that message loop in the thread you created
in the Load event handler?
Usually if you need to create UI thread (thread running a message loop) you
create a form. Ofcourse you don't have to have form to send messages(I've
never tried to send thread messages in .NET so, I don't know how it handles
them)

So way you need those messahe loop. May be there is another solution to your
problem.

HTH
B\rgds
100

"Franco, Gustavo" <gu************@hotmail.com> wrote in message
news:Oy**************@TK2MSFTNGP10.phx.gbl...
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());
}

}
}

Nov 15 '05 #2
Hi thanks for the answer,

Why the Callback function WaitProc is executed on a new thread instead from
where it was created? (Ok, I guess the answer is .Net framework design)

I need because is a BackgroundClass (It doesn't have Form in at all, and it
can't have it) that is used to process AyncSockets and them requiere of a
messageloop to process the Callback functions.

(Just in case :) ) I don't want to use Sync Socket, Async Socket are more
efficients specially when you have a pool of them, using Sync Socket
requiere more work when is a Server Pool because for Aync Socket the State
Machine is easier to control than Sync Sockets.

Now, to the original question.

How can I execute a method on one specific thread? This thread doesn't
inherit from Form, so I can't user Invoke.Require/Invoke.

Thanks,
Gustavo.

"100" <10*@100.com> wrote in message
news:uP**************@TK2MSFTNGP10.phx.gbl...
Hi Franco,

The problem is that your WaitProc is executed in the worker thread from the thread pool (not in the thread you create in the Form1_Load that runs the
message loop).
Application.ExitThread exits the message loop in the current thread. When
called form the worker thread (from the thread pool) there is no message
loop to exit from.

My question is why do you need that message loop in the thread you created
in the Load event handler?
Usually if you need to create UI thread (thread running a message loop) you create a form. Ofcourse you don't have to have form to send messages(I've
never tried to send thread messages in .NET so, I don't know how it handles them)

So way you need those messahe loop. May be there is another solution to your problem.

HTH
B\rgds
100

"Franco, Gustavo" <gu************@hotmail.com> wrote in message
news:Oy**************@TK2MSFTNGP10.phx.gbl...
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());
}

}
}


Nov 15 '05 #3
Somebody knows what is doing Form.InvokeRequired/Form.Invoke on
System.Windows.Forms.

Sombody has a Decompiler to take a look of it?

Thanks,
Gustavo.

P.S.: I tried use background.GetType().InvokeMember("UnloadClass), but it
calls the method on the calling thread and not on the thread where the class
were created.

"100" <10*@100.com> wrote in message
news:uP**************@TK2MSFTNGP10.phx.gbl...
Hi Franco,

The problem is that your WaitProc is executed in the worker thread from the thread pool (not in the thread you create in the Form1_Load that runs the
message loop).
Application.ExitThread exits the message loop in the current thread. When
called form the worker thread (from the thread pool) there is no message
loop to exit from.

My question is why do you need that message loop in the thread you created
in the Load event handler?
Usually if you need to create UI thread (thread running a message loop) you create a form. Ofcourse you don't have to have form to send messages(I've
never tried to send thread messages in .NET so, I don't know how it handles them)

So way you need those messahe loop. May be there is another solution to your problem.

HTH
B\rgds
100

"Franco, Gustavo" <gu************@hotmail.com> wrote in message
news:Oy**************@TK2MSFTNGP10.phx.gbl...
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());
}

}
}


Nov 15 '05 #4
100
Franko,
Ok, lets get that straight. Classes don't belong to threads. So where you
create you classes has nothing to do with, which thread executes the method
in that classes. .Wndows Form controls classes as classes don't belong to
any thread either. Windows OS control, though, belong to threads. Windows
Form controls use internally Windows OS controls so they kind of belong to
the threads when it comes to updating the UI. That's why controls need those
Invoke and InvokeRequired members because if you want to update a control
you have to do it form the UI thread. Invoke just executes a method in the
UI thread created the control. Internally Invoke uses SendMessage, I
believe, it takes care of switching the threads.

Back to your question. If you want to execute a method in different thread
you have bunch of choices.
You already used the main two:
1. Create Thread object and call its Start method. The method you pass to
the constructor will run in a separate worker thread.
2. Use thread from the thread pool. You are using one of the methods for run
method by the thread from the thread pool (WaitProc).

If you use .NET socket class as AsyncSocket (BeginReceive and BeginSentTo)
you can provide a callback method. That call beck method will be executed in
a separate worker thread. And won't block the thread that has started the
receiving operation. With that class you don't need to start any message
loops. The framework takes care of all details.

HTH
B\rgds
100

"Franco, Gustavo" <gu************@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
Somebody knows what is doing Form.InvokeRequired/Form.Invoke on
System.Windows.Forms.

Sombody has a Decompiler to take a look of it?

Thanks,
Gustavo.

P.S.: I tried use background.GetType().InvokeMember("UnloadClass), but it
calls the method on the calling thread and not on the thread where the class were created.

"100" <10*@100.com> wrote in message
news:uP**************@TK2MSFTNGP10.phx.gbl...
Hi Franco,

The problem is that your WaitProc is executed in the worker thread from

the
thread pool (not in the thread you create in the Form1_Load that runs the
message loop).
Application.ExitThread exits the message loop in the current thread. When called form the worker thread (from the thread pool) there is no message
loop to exit from.

My question is why do you need that message loop in the thread you created in the Load event handler?
Usually if you need to create UI thread (thread running a message loop)

you
create a form. Ofcourse you don't have to have form to send messages(I've never tried to send thread messages in .NET so, I don't know how it

handles
them)

So way you need those messahe loop. May be there is another solution to

your
problem.

HTH
B\rgds
100

"Franco, Gustavo" <gu************@hotmail.com> wrote in message
news:Oy**************@TK2MSFTNGP10.phx.gbl...
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());
}

}
}



Nov 15 '05 #5
Hi,

Thanks for the answer after read your email I see

1) I missed the MSDN section where it says,

"Calling the BeginSend method gives you the ability to send data within a
separate execution thread."

Then instead Application.Run() I called

AutoResetEvent checkEvent = new AutoResetEvet(false)

thread creation

thread.start

bla bla bla...

chechEvent.WaitOne();

object.dispose();

bla bla bla.

Cool, this worked fine, Thread was blocked and the Callback was executed on
different thread.

With this I could resolve the problem how to exit thread and dispose my
object right.

2) Execute a method on another thread.

Usually it can be something very useful for me because I have 6 threads
running.

Each thread creates a main object

Each thread expose the main object which I can use to access to his public
methods from different threads

2a) When I want to execute the method in a specific object/thread (in my
case the relation is one to one, each thread create his own object), the
thread is already created and I can't send the method to the constructor
because it is already created.

mmm... this case is not useful for me.

2b) Use thread from the thread pool like you said before with WaitProc, that
means on my call to my own "Invoke" a can send like a parameter my method to
execute and inside it signal the Event, and on the new thread (WaitProc)
execute the method.

Question:

For each call to my own Invoke is going to take a new thread from the thread
pool, is not a overhead for the framework? "I know it works"

2c) If Invoke take care of the switching then I changed a little bit my
example and in my example a inherit BackgroundClass from Control.

Create a delegate UnloadClassDelegate and point it to UnloadClassInternal

On UnloadClass I did:

Public void UnloadClass()

{

Console.WriteLine("Current Thread UnloadClass() : " +
Thread.Cur.....GetHas....

if (this.InvokeRequired)

this.Invoke(mUnloadClassDelegate)

else

UnloadClassInternal();

}

Private void UnloadClassInternal()

{

Console.WriteLine("Current Thread UnloadClassInternal() : " +
Thread.Cur.....GetHas....

}

When I execute this, doesn't care from where I call UnloadClass() always
UnloadClassInternal() is executed on the thread where the Control where
created and this not take a new thread from the thread pool. So, like you
said Invoke use SendMessage to do that.

Then if the point 2b) is positive and there is a little overhead the point
2c) is a solution to it?

Thanks,

You were useful to clear all this.
"100" <10*@100.com> wrote in message
news:#B*************@TK2MSFTNGP12.phx.gbl...
Franko,
Ok, lets get that straight. Classes don't belong to threads. So where you
create you classes has nothing to do with, which thread executes the method in that classes. .Wndows Form controls classes as classes don't belong to
any thread either. Windows OS control, though, belong to threads. Windows
Form controls use internally Windows OS controls so they kind of belong to the threads when it comes to updating the UI. That's why controls need those Invoke and InvokeRequired members because if you want to update a control
you have to do it form the UI thread. Invoke just executes a method in the
UI thread created the control. Internally Invoke uses SendMessage, I
believe, it takes care of switching the threads.

Back to your question. If you want to execute a method in different thread
you have bunch of choices.
You already used the main two:
1. Create Thread object and call its Start method. The method you pass to
the constructor will run in a separate worker thread.
2. Use thread from the thread pool. You are using one of the methods for run method by the thread from the thread pool (WaitProc).

If you use .NET socket class as AsyncSocket (BeginReceive and BeginSentTo)
you can provide a callback method. That call beck method will be executed in a separate worker thread. And won't block the thread that has started the
receiving operation. With that class you don't need to start any message
loops. The framework takes care of all details.

HTH
B\rgds
100

"Franco, Gustavo" <gu************@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
Somebody knows what is doing Form.InvokeRequired/Form.Invoke on
System.Windows.Forms.

Sombody has a Decompiler to take a look of it?

Thanks,
Gustavo.

P.S.: I tried use background.GetType().InvokeMember("UnloadClass), but it
calls the method on the calling thread and not on the thread where the

class
were created.

"100" <10*@100.com> wrote in message
news:uP**************@TK2MSFTNGP10.phx.gbl...
Hi Franco,

The problem is that your WaitProc is executed in the worker thread from
the
thread pool (not in the thread you create in the Form1_Load that runs the message loop).
Application.ExitThread exits the message loop in the current thread. When called form the worker thread (from the thread pool) there is no
message loop to exit from.

My question is why do you need that message loop in the thread you

created in the Load event handler?
Usually if you need to create UI thread (thread running a message loop) you
create a form. Ofcourse you don't have to have form to send messages(I've never tried to send thread messages in .NET so, I don't know how it

handles
them)

So way you need those messahe loop. May be there is another solution
to
your
problem.

HTH
B\rgds
100

"Franco, Gustavo" <gu************@hotmail.com> wrote in message
news:Oy**************@TK2MSFTNGP10.phx.gbl...
> 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());
> }
>
>
>
> }
> }
>
>
>
>



Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
6
by: What-a-Tool | last post by:
I'm going out out of my mind trying to get this to work with no luck. The error message I get is at the bottom. Can someone please tell me what I'm doing wrong here. I've tried this a million...
39
by: Scotter | last post by:
Okay I think my title line was worded misleadingly. So here goes again. I've got quite 20 identical MDB files running on an IIS5 server. From time to time I need to go into various tables and add...
5
by: TrvlOrm | last post by:
HI There, I have been struggling with JavaScript code for days now, and this is my last resort! Please help... I am trying to create a JavaScript slide show with links for Next Slide,...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
22
by: rasiel | last post by:
I'm hoping someone can help me out. I'm a researcher in need of developing an automated database and would like to see if someone here is willing to consider putting together for me a simple...
17
by: Amy | last post by:
Hi, I finished this script and for some reason there is a delay every so often in the timing. Sometimes it seems two take 2 seconds instead of 1. Can anyone see anything that would slow it down? I...
9
by: FERHAT AÇICI | last post by:
hi all! who know arrays on visual basic please tell me.... thanks..
22
by: Amali | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of...
112
by: Prisoner at War | last post by:
Friends, your opinions and advice, please: I have a very simple JavaScript image-swap which works on my end but when uploaded to my host at http://buildit.sitesell.com/sunnyside.html does not...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.