View Single Post
Old 05-17-2007   #4 (permalink)
Oisin Grehan


 
 

Re: Hide a Windows form

Don, unfortunately it ain't that easy:

a) Powershell does not support event handling natively.

Yes, it will let you assign scriptblocks as handlers for events via
the add_ and remove_ syntax, but when it comes to execute the
scriptblock, it will fail because of a lack of a context runspace.
This is something half-baked in powershell, and will probably be fixed
for vNext. In the meantime, you can bind to events and handle them in
a pseudo-realtime fashion using http://www.codeplex.com/PSEventing

b) Windows forms programming is highly unstable in powershell due to
the multithreaded nature of the runspace/pipeline architecture.

Windows Forms components demand that the same thread that created them
be the one that manipulates them. Since every pipeline is assigned to
a different worker thread under powershell, thread affinity is not
gauranteed when playing around like this. Results vary from "it works
sometimes" to "oops, powershell has locked up."

While pseventing can partially solve problem a), it can do nothing to
help with part b).

- Oisin

p.s. you're the vbscript guy, right?

On May 15, 5:30 pm, "Don Jones [MVP]" <d...@sapien.com> wrote:
> So, event driven programming can be tricky.
>
> When you call ShowDialog(), you're showing the form modally, which means
> PowerShell "waits" until the form closes. Once the form closes, PowerShell
> can run again... but that that point, the *form* is no longer "running."
> Since ShowDialog() is the last line in your script, once the form is hidden,
> your script ends.
>
> You have options. I would recommend simply setting the form's Visible
> property. That'll leave your script running, but should control the visible
> appearance of the form - while leaving the form "open."
>
> You've got some other things going on. For one, you do know that your timer
> isn't being added to the form, right? Not sure if that's your intent or not.
>
> --
> Don Jones
> Windows PowerShell MVP
> Founder:www.ScriptingAnswers.com
> Co-Author: "Windows PowerShell: TFM"
>
> "Pav" <npawlac...@gmail.com> wrote in message
>
> news:1179242034.407687.323420@n59g2000hsh.googlegroups.com...
>
>
>
> > Hello,

>
> > I have a powershell app that i'd like to hide/show with a systray icon
> > menu. Unfortunately, My script quits as soon as i call the Hide()
> > method of the form.
> > I'm not really experienced in .NET windows classes, Perhaps the
> > problem doesn't involve powershell at all. If so, feel free to
> > redirect me toward the good group.

>
> > Here's the main structure of my script :

>
> > ----- cut here ------
> > $form = new-object
> > System.Windows.Forms.form

>
> > $mi = new-object System.Windows.Forms.MenuItem
> > $mi.Index = 0
> > $mi.Text = "Hide"
> > $mi.add_Click({

>
> > $form.hide()
> > })

>
> > $cm = new-object System.Windows.Forms.ContextMenu
> > $form.contextMenu = $cm
> > $form.contextMenu.MenuItems.AddRange($mi)

>
> > $form.ShowInTaskbar = $true
> > $form.set_autosize($true)
> > $form.Text = "My Form"

>
> > $ni = new-object System.Windows.Forms.NotifyIcon
> > $ni.Icon = New-object System.Drawing.Icon("icon.ico")
> > $NI.ContextMenu = $cm
> > $NI.Text = "Systray Text"
> > $NI.Visible =
> > $True;

>
> > $timer = New-Object System.Windows.Forms.Timer
> > $timer.Interval = 5000

>
> > $timer.add_Tick({
> > #...doing my treatment
> > })

>
> > $timer.Start()

>
> > $form.showdialog()
> > ----- cut here ------

>
> > Any ideas ?
> > Thanks,

>
> > Pav- Hide quoted text -

>
> - Show quoted text -



My System SpecsSystem Spec