About MultiThread in Form

C

cok

Hi ,All

I'm a newbie for c#, I have a question about MultiThread in C# and Form,

In .Net 2.0, the Form cann't be access by other thread, but
when I start a new thread using follow code:

Thread newThread = new Thread(MyFormInstance.method)

compile and run OK,
why this Form instance can be access by two thread(main thread and
newThread)
and no exception occured?

thanks for any advice

--
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
_/
_/ Nie Longhai , coder -_-|||
_/ Shanghai Xomi Instruments Co., Ltd.
_/ URL: http://www.xomi.cn
_/ Shanghai C&U Industrial Park
_/ Feng Cheng Town, Feng Xian District
_/ Shanghai, 201411
_/ Phone:86-21-57513966-807
_/ Fax:86-21-57513636
_/ Mobile:13162055440
_/ Email:[email protected] ,[email protected]
_/
_/ Profession & Focus
_/ High precision semiconductor metrology system vendor.
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
 
J

Jon Shemitz

cok said:
In .Net 2.0, the Form cann't be access by other thread, but
when I start a new thread using follow code:

Thread newThread = new Thread(MyFormInstance.method)

compile and run OK,
why this Form instance can be access by two thread(main thread and
newThread)
and no exception occured?

The rule is simple: You can only access a Control from the thread that
created it. The only exceptions are that you can always Invoke (or
BeginInvoke) a delegate, and you can always CreateGraphics.

However, this is NOT a matter of "you will always get an exception if
you change a Control's properties from another thread." It's a much
more complicated matter of "it will work most of the time, but will
blow up, occasionally, when you try to do two things at once." That's
why you should never change a Control's properties from a secondary
thread.

Also, the potential problems are with GUI access. The prohibition on
secondary thread access only applies to Control (Form) members, not to
any non-Control members you added to your specific Control or Form.
You can freely access app-level members of forms (or of, say, your
TreeNode descendant classes) from secondary threads, subject only to
normal synchronization issues.

So, finally specifically answering your question, there is no problem
running a delegate to a form method in a thread. The only problem is
if that form method changes form properties without using Invoke or
BeginInvoke, or if that form method doesn't use synchronization logic
(like the lock statement) to avoid race conditions.

--

..NET 2.0 for Delphi Programmers <http://www.midnightbeach.com/.net>

Delphi skills make .NET easy to learn
Just printed, and shipping now.
 
C

cok

Hi, Jon

thank you for your help.

Jon Shemitz said:
The rule is simple: You can only access a Control from the thread that
created it. The only exceptions are that you can always Invoke (or
BeginInvoke) a delegate, and you can always CreateGraphics.

However, this is NOT a matter of "you will always get an exception if
you change a Control's properties from another thread." It's a much
more complicated matter of "it will work most of the time, but will
blow up, occasionally, when you try to do two things at once." That's
why you should never change a Control's properties from a secondary
thread.

Also, the potential problems are with GUI access. The prohibition on
secondary thread access only applies to Control (Form) members, not to
any non-Control members you added to your specific Control or Form.
You can freely access app-level members of forms (or of, say, your
TreeNode descendant classes) from secondary threads, subject only to
normal synchronization issues.

So, finally specifically answering your question, there is no problem
running a delegate to a form method in a thread. The only problem is
if that form method changes form properties without using Invoke or
BeginInvoke, or if that form method doesn't use synchronization logic
(like the lock statement) to avoid race conditions.

--

.NET 2.0 for Delphi Programmers <http://www.midnightbeach.com/.net>

Delphi skills make .NET easy to learn
Just printed, and shipping now.
 

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