We are building Chat like application using Forms and as a result our
programming is becoming complicated to display messages received on
different threads in the chat window (due to STA requirements of Forms). Is
there a way to build Chat like application WITHOUT using FORM, so that one
can use free threading model (The link
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcondevelopingmultithreadedwindowsformscontrol.asp
has statement "Outside Windows Forms, classes in the .NET Framework use the
free threading model. For information about threading in the .NET Framework,
see Threading."). IS THERE ANY WAY TO BUILD CHAT-like application without
having to do complicated programming to satisfy STA requirements)?

Thanks in Advance,
Regards,
Mahesh

Re: Forms, multi-threading, Chats by Jochen

Jochen
Mon May 09 00:49:57 CDT 2005

Hi Mahesh!

> We are building Chat like application using Forms and as a result our
> programming is becoming complicated to display messages received on
> different threads in the chat window (due to STA requirements of Forms). Is
> there a way to build Chat like application WITHOUT using FORM, so that one
> can use free threading model (The link
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcondevelopingmultithreadedwindowsformscontrol.asp
> has statement "Outside Windows Forms, classes in the .NET Framework use the
> free threading model. For information about threading in the .NET Framework,
> see Threading."). IS THERE ANY WAY TO BUILD CHAT-like application without
> having to do complicated programming to satisfy STA requirements)?

As long as you do not have any GUI, it is very easy.
But if you want to display it in some Windows-GUI, then you are limited
(in most cases) to single-threaded (or to be more specific: (Most)
messages to a window must be sent from the same thread, that created the
window).
Sometimes there are also two different functions (like: ShowWindow and
ShowWindowAsync).

From my point of view: Working with Windows-Forms is very easy (even
with multi-threaded; the approch of "BeginInvoke" and "Invoke" is
streight forward).

See: Multiple Threads in the User Interface
http://msdn.microsoft.com/library/en-us/dndllpro/html/msdn_winthr.asp

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

Re: Forms, multi-threading, Chats by Jon

Jon
Mon May 09 00:56:49 CDT 2005

Mahesh Devjibhai Dhola [MVP] <dholamahesh@hotmail.com> wrote:
> We are building Chat like application using Forms and as a result our
> programming is becoming complicated to display messages received on
> different threads in the chat window (due to STA requirements of Forms).

It's got nothing to do with the STA requirement of Forms - it's a
fundamental Windows GUI programming requirement that you only access a
UI component on the thread which created it.

Howveer, it shouldn't be particularly complicated to update the UI
appropriately.

See http://www.pobox.com/~skeet/csharp/threads/winforms.shtml

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: Forms, multi-threading, Chats by Ioannis

Ioannis
Mon May 09 06:59:58 CDT 2005

Jon Skeet [C# MVP] wrote:

> It's got nothing to do with the STA requirement of Forms - it's a
> fundamental Windows GUI programming requirement that you only access a
> UI component on the thread which created it.
>
> Howveer, it shouldn't be particularly complicated to update the UI
> appropriately.
>
> See http://www.pobox.com/~skeet/csharp/threads/winforms.shtml


I am currently reading .NET networking (and have read .NET multithreading in the past),
and I see no problem updating GUI components from different threads. In addition, STA etc
have nothing to do with pure .NET applications (applications that do not use COM and other
past APIs).

For example we can update a TextBox from different threads upon receiving or sending data
(in the chat client example). In fact this is what I am currently reading about, Chat
client/server applications using connection (TCP/IP stream sockets) and connectionless
(UDP) String exchanges.


Am I missing something?


Re: Forms, multi-threading, Chats by Willy

Willy
Mon May 09 07:12:44 CDT 2005


"Ioannis Vranos" <ivr@remove.this.grad.com> wrote in message
news:OpWTf6IVFHA.2664@TK2MSFTNGP15.phx.gbl...
> Jon Skeet [C# MVP] wrote:
>
>> It's got nothing to do with the STA requirement of Forms - it's a
>> fundamental Windows GUI programming requirement that you only access a UI
>> component on the thread which created it.
>>
>> Howveer, it shouldn't be particularly complicated to update the UI
>> appropriately.
>>
>> See http://www.pobox.com/~skeet/csharp/threads/winforms.shtml
>
>
> I am currently reading .NET networking (and have read .NET multithreading
> in the past), and I see no problem updating GUI components from different
> threads. In addition, STA etc have nothing to do with pure .NET
> applications (applications that do not use COM and other past APIs).
>
> For example we can update a TextBox from different threads upon receiving
> or sending data (in the chat client example). In fact this is what I am
> currently reading about, Chat client/server applications using connection
> (TCP/IP stream sockets) and connectionless (UDP) String exchanges.
>
>
> Am I missing something?
>

Yes, a fundamental rule in Windows that say's that Windows handles (HWND's)
have thread affinity, that means they should not be used from another thread
than the owning thread. Note that v2.0 contains a debug user probe that
warns you about cross-thread access to window UI elements.
Also note that the UI thread needs to run in a STA because some of the UI
controls might use COM under the covers. Another reason to have the thread
running in a STA is that and Cut/Paste (Clipboard access) is using OLE based
and needs a STA to run.

Willy.



Re: Forms, multi-threading, Chats by Jon

Jon
Mon May 09 12:51:16 CDT 2005

Ioannis Vranos <ivr@remove.this.grad.com> wrote:
> I am currently reading .NET networking (and have read .NET
> multithreading in the past), and I see no problem updating GUI
> components from different threads.

Then you haven't read the documentation clearly enough. From
Control.Invoke:

<quote>
Note There are four methods on a control that are safe to call from
any thread: Invoke, BeginInvoke, EndInvoke, and CreateGraphics. For all
other method calls, you should use one of the invoke methods to marshal
the call to the control's thread.
</quote>

> In addition, STA etc have nothing to do with pure .NET applications
> (applications that do not use COM and other past APIs).

Many Windows Forms controls use COM components internally.

> For example we can update a TextBox from different threads upon
> receiving or sending data (in the chat client example).

No you can't - not reliably. It may happen to work for you at the
moment, but you shouldn't do it.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: Forms, multi-threading, Chats by Dennis

Dennis
Mon May 09 20:38:01 CDT 2005

Just curious as to the STA abbreviation...what does this refer to. Thanks
for any explaination as I've just started learning VB.Net

"Jon Skeet [C# MVP]" wrote:

> Ioannis Vranos <ivr@remove.this.grad.com> wrote:
> > I am currently reading .NET networking (and have read .NET
> > multithreading in the past), and I see no problem updating GUI
> > components from different threads.
>
> Then you haven't read the documentation clearly enough. From
> Control.Invoke:
>
> <quote>
> Note There are four methods on a control that are safe to call from
> any thread: Invoke, BeginInvoke, EndInvoke, and CreateGraphics. For all
> other method calls, you should use one of the invoke methods to marshal
> the call to the control's thread.
> </quote>
>
> > In addition, STA etc have nothing to do with pure .NET applications
> > (applications that do not use COM and other past APIs).
>
> Many Windows Forms controls use COM components internally.
>
> > For example we can update a TextBox from different threads upon
> > receiving or sending data (in the chat client example).
>
> No you can't - not reliably. It may happen to work for you at the
> moment, but you shouldn't do it.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
>

Re: Forms, multi-threading, Chats by Jon

Jon
Tue May 10 01:23:49 CDT 2005

Dennis <Dennis@discussions.microsoft.com> wrote:
> Just curious as to the STA abbreviation...what does this refer to. Thanks
> for any explaination as I've just started learning VB.Net

Single Threaded Apartment. I'm far from an expert on apartments, but
basically COM components can declare that they need to be run on a
single thread, with calls being marshalled to that thread. Hopefully a
search for "single threaded apartment" will get you more information
than that :)

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: Forms, multi-threading, Chats by Dennis

Dennis
Tue May 10 19:09:01 CDT 2005

Thanks..at least I know where to start looking.

"Jon Skeet [C# MVP]" wrote:

> Dennis <Dennis@discussions.microsoft.com> wrote:
> > Just curious as to the STA abbreviation...what does this refer to. Thanks
> > for any explaination as I've just started learning VB.Net
>
> Single Threaded Apartment. I'm far from an expert on apartments, but
> basically COM components can declare that they need to be run on a
> single thread, with calls being marshalled to that thread. Hopefully a
> search for "single threaded apartment" will get you more information
> than that :)
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
>