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

Calling a UI Class From a Non-UI Class.

Hi,

As a matter of principle I have refranied from creating an instance of
Form or UserControl from within a class that does not inherit from
System.Windows.Form.

I am looking for comments on Pros and Cons of the approach.

My logic has been that UI is always executed on the main application
thread. I use Control.InvokeRequired or delegates to ensure that I am
always changing something on the UI in the context of the main
thread.

If I were to create an instance of form in a non-ui class (one that
does not inherit from System.Windows.Form) I am inviting potential
trouble where someone may instantiate the non-ui class on a worker
thread which in turn will instantiate a form. Now we are in a
situation that we have a form that is created on worker thread.

Please put u'r comments on this subject. Is it a hard and fast rule
that create an instance of a UI/Form object only on the main thread?

Feb 15 '07 #1
1 6159
Technically Forms have no tie whatsoever to the 'main' thread. The rule is,
you should mess with a window handle on another thread to the one that
created it. Most windows forms have a main form which was created on the
entry thread, therefore they are tied to ui processing on that thread. I have
an application launcher class in a UIFramework dll which has no base class
but controls the display of a splash screen for all my apps when they load.
It creates the splash screen in a secondary thread and thats fine as I
marshal all calls to the form onto that same secondary thread while my main
thread goes about loading the main form.
So the hard and fast rule is not to use a window handle (thats for forms and
controls) on a thread that didnt create it. if you might be on a thread that
didnt create the handle, ensure you always check Control.InvokeRequired and
use the Control.Invoke function to marshal calls onto the correct thread when
InvokeRequired is true.

HTH
--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"by******@gmail.com" wrote:
Hi,

As a matter of principle I have refranied from creating an instance of
Form or UserControl from within a class that does not inherit from
System.Windows.Form.

I am looking for comments on Pros and Cons of the approach.

My logic has been that UI is always executed on the main application
thread. I use Control.InvokeRequired or delegates to ensure that I am
always changing something on the UI in the context of the main
thread.

If I were to create an instance of form in a non-ui class (one that
does not inherit from System.Windows.Form) I am inviting potential
trouble where someone may instantiate the non-ui class on a worker
thread which in turn will instantiate a form. Now we are in a
situation that we have a form that is created on worker thread.

Please put u'r comments on this subject. Is it a hard and fast rule
that create an instance of a UI/Form object only on the main thread?

Feb 16 '07 #2

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

Similar topics

12
by: bhennon | last post by:
Hey all, I have a small php script that calls a random image at the following page. http://www.2006ymcanationals.com/random.php IT WORKS IF I go directly to the above link. I am trying to...
1
by: Ian Sedwell | last post by:
Hi guys Many thanks to all who replied to my original question. Actually, it's dead easy and the way I was doing it was correct the first time. You do indeed simply call the VBScript routine...
2
by: Giulio Belrango | last post by:
Hi I need someones help I'm working in an IBM 390 batch environment. What I'm trying to do is to call from a non DB2 COBOL program a DB2 COBOL program that will access a table and perform an...
5
by: Dave | last post by:
does calling a regular function cost any cpu time? In other words, is it faster to write the code of two functions into main(), or is it the exact same thing as calling two functions. I know its...
0
by: monika.saxena | last post by:
Hi all, In one of my projects which is a web based application in asp.net, a third party tool - "Frontline Solver DLL" (It is an unmanaged DLL and ..NET is calling it using the PInvoke) is used....
4
by: Martin Feuersteiner | last post by:
Dear Group I'm using VB to write an aspnet application. I would like to call a javascript function from within a VB Sub or VB Function, is it possible? My code is something like this: VB...
4
by: Jeronimo Bertran | last post by:
Hello, I have a WCF service that I am using to upload files to a server by using the streamed transfer method. I am currently calling the service from a WCF client. All I did was add a Service...
47
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both...
10
by: sulekhasweety | last post by:
Hi, the following is the definition for calling convention ,which I have seen in a text book, can anyone give a more detailed explanation in terms of ANSI - C "the requirements that a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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.