Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / June 2004

Tip: Looking for answers? Try searching our database.

How to Invoke when form is hidden?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Altramagnus - 29 Jun 2004 03:42 GMT
I have a main thread which creates all my forms.
All my forms are hidden initially.
After that, I have some worker threads which will call BeginInvoke
to update and show the necessary forms.
However, it throws an exception saying that a handle is needed to be
created in order to use Invoke or BeginInvoke.
But the handle is only created when the window is first visible!!!!!!!!
How then can I update and show the form when it is initially invisible??????

Somebody please HELP!
This .NET problem is killing me!
NOTE: JAVA HAS NO SUCH PROBLEM!!!!!
Why tie a window to a thread!?!?!?
Why create a handle ONLY when it is visible?
Jay B. Harlow [MVP - Outlook] - 29 Jun 2004 04:44 GMT
Altramagnus,
> NOTE: JAVA HAS NO SUCH PROBLEM!!!!!
Then use Java! ;-)

> Why tie a window to a thread!?!?!?
Its a Win32 limitation, System.Windows.Forms is based on (thin wrappers of)
Win32 Windows.

> Why create a handle ONLY when it is visible?
You can use Control.IsHandleCreated to see if the handle is created yet or
not. You should be able to use Control.CreateControl to create the handle,
if its not yet created.

My concern would be using CreateControl from a worker thread, as that may
cause the BeginInvoke to need to be handled on the worker thread and not the
main thread. I have not actually used forms on multiple threads, so I'm not
sure what will happen... I would probably simply call CreateControl on the
main thread, so there's no question.

Hope this helps
Jay

> I have a main thread which creates all my forms.
> All my forms are hidden initially.
[quoted text clipped - 10 lines]
> Why tie a window to a thread!?!?!?
> Why create a handle ONLY when it is visible?
Altramagnus - 29 Jun 2004 06:13 GMT
I did try calling CreateControl
But the handle is still not created if it is invisible.
so it is still a problem.
I have to call .Show() or Visible = true in order for the handle to be
created.

> Altramagnus,
> > NOTE: JAVA HAS NO SUCH PROBLEM!!!!!
[quoted text clipped - 33 lines]
> > Why tie a window to a thread!?!?!?
> > Why create a handle ONLY when it is visible?
Jay B. Harlow [MVP - Outlook] - 29 Jun 2004 15:28 GMT
Altramagnus,
Its nice to know that Control.CreateControl, doesn't actually create the
handle, as MSDN would suggest it does...

Now I'm curious on when one would actually use Control.CreateControl.

I'll see what I can find out.

Hope this helps
Jay

> I did try calling CreateControl
> But the handle is still not created if it is invisible.
[quoted text clipped - 42 lines]
> > > Why tie a window to a thread!?!?!?
> > > Why create a handle ONLY when it is visible?
Jay B. Harlow [MVP - Outlook] - 29 Jun 2004 15:48 GMT
Altramagnus,
I did some experimenting.

If you call Form.Handle, the handle will be created for you.

       Debug.WriteLine(form.IsHandleCreated, "IsHandleCreated")
       Debug.WriteLine(form.Handle.ToInt32(), "Handle")
       form.CreateControl()
       Debug.WriteLine(form.IsHandleCreated, "IsHandleCreated")
       Debug.WriteLine(form.Handle.ToInt32(), "Handle")

If I take out the calls to form.Handle, I get the effect you describe, if I
leave them in the handle is created. I am checking on when you would
actually use CreateControl, as it appears (as you stated) not to do anything
for a Form.

FWIW: I used "form.Handle.ToInt32()" to avoid the debugger from displaying
the value of the Handle itself, invalidating my test...

Hope this helps
Jay

> I did try calling CreateControl
> But the handle is still not created if it is invisible.
[quoted text clipped - 42 lines]
> > > Why tie a window to a thread!?!?!?
> > > Why create a handle ONLY when it is visible?
Altramagnus - 29 Jun 2004 06:18 GMT
> Altramagnus,
> > NOTE: JAVA HAS NO SUCH PROBLEM!!!!!
> Then use Java! ;-)

Yes I would love to.
But it is not up to me to decide.
My customer insist on C#

> > Why tie a window to a thread!?!?!?
> Its a Win32 limitation, System.Windows.Forms is based on (thin wrappers of)
> Win32 Windows.

> > Why create a handle ONLY when it is visible?
> You can use Control.IsHandleCreated to see if the handle is created yet or
> not. You should be able to use Control.CreateControl to create the handle,
> if its not yet created.

I tried this:

Form f = new Form();
Console.WriteLine( f.IsHandleCreated );
f.CreateControl();
Console.WriteLine( f.IsHandleCreated );
f.Show();
Console.WriteLine( f.IsHandleCreated );

Output is:
false
false
true

so handle is only created when it is first shown.

> My concern would be using CreateControl from a worker thread, as that may
> cause the BeginInvoke to need to be handled on the worker thread and not the
> main thread. I have not actually used forms on multiple threads, so I'm not
> sure what will happen... I would probably simply call CreateControl on the
> main thread, so there's no question.

> Hope this helps
> Jay
[quoted text clipped - 14 lines]
> > Why tie a window to a thread!?!?!?
> > Why create a handle ONLY when it is visible?
Herfried K. Wagner [MVP] - 29 Jun 2004 08:59 GMT
* altramagnus@hotmail.com (Altramagnus) scripsit:
> I have a main thread which creates all my forms.
> All my forms are hidden initially.
[quoted text clipped - 4 lines]
> But the handle is only created when the window is first visible!!!!!!!!
> How then can I update and show the form when it is initially invisible??????

In this case 'Control.InvokeRequired' will return 'False' and you can
access the controls directly.  Nevertheless, you will have to make sure
that the state of the UI doesn't change (the form is not shown), because
this may require to use invoke.

Signature

Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/

Altramagnus - 29 Jun 2004 10:43 GMT
> * altramagnus@hotmail.com (Altramagnus) scripsit:
> > I have a main thread which creates all my forms.
[quoted text clipped - 10 lines]
> that the state of the UI doesn't change (the form is not shown), because
> this may require to use invoke.

Thanks for the info.
I did not realize that Control.InvokeRequired will return False when handle
is not created yet.
This solves the updating part. But I am required to show the form after
updating.

When the form is initially hidden, the handle is not created, I could not
call invoke from a worker thread
to show it, because without a handle, invoke cannot be called.
I could not call .Show() directly from the work thread either, because that
will tie the window to my worker thread.

Then how do I change the visibility with my worker thread ( intially hidden
when handle is not created )?
why can't we force the handle to be created when the form is invisible?
Isn't that what the CreateControl() should do?

Thanks.
Fran?ois J. Miton - 29 Jun 2004 14:02 GMT
Hi,

An alternative solution to calling Invoke would be to raise an event and
have your forms do what they have to do in the event handler. The event
machinery will cope with thread marshalling issues.

HTH

FM
fmiton@online.microsoft.com

> I have a main thread which creates all my forms.
> All my forms are hidden initially.
[quoted text clipped - 10 lines]
> Why tie a window to a thread!?!?!?
> Why create a handle ONLY when it is visible?
Altramagnus - 30 Jun 2004 03:23 GMT
How do you raise an event from another thread to the thread that ties to the
window?

Thanks.

> Hi,
>
[quoted text clipped - 22 lines]
> > Why tie a window to a thread!?!?!?
> > Why create a handle ONLY when it is visible?

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.