Thread crossing

L

Laurent Navarro

Hello,


I created an application with the FrameWork 1.1 containing differents
forms and threads runing. There was no problem, I could click on a picture
on my Main Form to call the Show() function of another form without problem.

Then I installed Visual Studio 2005 and the FrameWork 2.0. I launched my
application and I had lots of error saying I was crossing the thread calls
when clicking on the picture of my Main Form or calling the BeginInit()
function of a custom control containing a TreeView.

I read the MSDN and found the
System.Windows.Forms.CheckForIllegalCrossThreadCalls parameter and set it to
false. It is now working but I know that it is just a trick hiding some
mistakes in my source code.

My question is: do I have to create delegates each time I want to use a
function of a form or a control from another form or thread ? If I want to
call the Show() function of a form from my Main Form, do I have to create on
the child form a delegate and a new NewShow() function like this (for
example):

public classe MyForm : System.Windows.Forms.Form
{
private delegate void show(void);

public void NewShow()
{
if (this.InvokeRequired)
{
this.Invoke(new show(base.Show(void)));
}
else
{
base.Show();
}
}
}

It is just an example, the syntaxe may be incorrect but the idea is
here. Prehaps did I miss something and I hope someone will be able to help
me !

Thank you

Laurent
 
W

Willy Denoyette [MVP]

| Hello,
|
|
| I created an application with the FrameWork 1.1 containing differents
| forms and threads runing. There was no problem, I could click on a picture
| on my Main Form to call the Show() function of another form without
problem.
|
| Then I installed Visual Studio 2005 and the FrameWork 2.0. I launched
my
| application and I had lots of error saying I was crossing the thread calls
| when clicking on the picture of my Main Form or calling the BeginInit()
| function of a custom control containing a TreeView.
|
| I read the MSDN and found the
| System.Windows.Forms.CheckForIllegalCrossThreadCalls parameter and set it
to
| false. It is now working but I know that it is just a trick hiding some
| mistakes in my source code.
|
| My question is: do I have to create delegates each time I want to use a
| function of a form or a control from another form or thread ? If I want to
| call the Show() function of a form from my Main Form, do I have to create
on
| the child form a delegate and a new NewShow() function like this (for
| example):
|
| public classe MyForm : System.Windows.Forms.Form
| {
| private delegate void show(void);
|
| public void NewShow()
| {
| if (this.InvokeRequired)
| {
| this.Invoke(new show(base.Show(void)));
| }
| else
| {
| base.Show();
| }
| }
| }
|
| It is just an example, the syntaxe may be incorrect but the idea is
| here. Prehaps did I miss something and I hope someone will be able to help
| me !
|
| Thank you
|
| Laurent
|
|

That means your application was buggy from the start, you should never touch
the UI from another thread than the creator of the UI elements (form,
controls). The V2 framework implements a debug probe to watch for this
common error.
This means that you need to call Control.Invoke or BeginInvoke whenever you
need to touch (update or read a property) from another thread.

Willy.
 
S

savia755

Willy said:
That means your application was buggy from the start, you should never touch
the UI from another thread than the creator of the UI elements (form,
controls). The V2 framework implements a debug probe to watch for this
common error.
This means that you need to call Control.Invoke or BeginInvoke whenever you
need to touch (update or read a property) from another thread.
I think I find the bug of my application by this advice. Very useful
though
 

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