473,372 Members | 1,636 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,372 software developers and data experts.

Forms, multi-threading, Chats

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/de...rmscontrol.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
Jul 21 '05 #1
8 1870
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/de...rmscontrol.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...sdn_winthr.asp

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Jul 21 '05 #2
Mahesh Devjibhai Dhola [MVP] <dh*********@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/t...winforms.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3
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/t...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?

Jul 21 '05 #4

"Ioannis Vranos" <iv*@remove.this.grad.com> wrote in message
news:Op**************@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/t...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.
Jul 21 '05 #5
Ioannis Vranos <iv*@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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #6
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 <iv*@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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #7
Dennis <De****@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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #8
Thanks..at least I know where to start looking.

"Jon Skeet [C# MVP]" wrote:
Dennis <De****@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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #9

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

Similar topics

0
by: Mike | last post by:
I have just installed Visual Studio .NET and all necessary Windows Updates. I can open my project in the IDE without any problems and can run it without any problems. My PC's OS is Windows 2000...
0
by: Zak | last post by:
Hi, I am a new comer to CGI.pm and find it extremely useful. I have created multi-form pages and with little struggle have succesfully been able to save states from one form to another (from one...
19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
2
by: pei_world | last post by:
Hi there, I have a application with multi-forms. The situation is that Form A(have a Dataset) is a base window,click one button to instiniate Form B(use the Database from Form A, Update the...
7
by: dave | last post by:
I have one application that is used within our intranet that places a forms auth cookie for our domain (for intranet purposes only). All other applications rely on this cookie for authentication and...
1
by: Tim Frawley | last post by:
I have a large form with many controls, almost like legal size paper in height. Just so I can head these off, I am not looking for suggestions to the effect of using Tab controls, multi-window...
15
by: Joshua Kendall | last post by:
I have a script in which it keeps opening the same form instead of only one instance. I also need help with a form that has a password. Where do I put the actual password? can I use a database for...
18
by: Jerry Boone | last post by:
I'm looking for a way to fire a public sub/function when a form is opened and closed - without using form level events. I have already done some extensive form work with instancing, looping...
5
by: c676228 | last post by:
Hi everyone, my colleagues are thinking about have three insurance plans on one asp page: I simplify the plan as follow: text box:number of people plan1 plan2 plan3
21
by: Dan Tallent | last post by:
In my application I have a form (Customer) that I want to be able to open multiple copies at once. Within this form I have other forms that can be opened. Example: ZipCode. When the user enters...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.