Check if form is Opened Winform / Powershell - winforms

I'm searching how I could check if a winform form is opened with Powershell, like this response for VB.net. I'm working with two runspaces, and I need to start the second, when my form is opened.
My first runspace is for the GUI. When the UI creation is completed, I opened it
$CommonHashTable.MainForm.ShowDialog()
And then, I'm trying to test if this form is opened (snipet from VB.net) from PowerShell main thread:
If Application.OpenForms().OfType(Of $CommonHashTable.MainForm).Any Then
... startsecondrunspace

A better way to test if the form is open might be to
if ($CommonHashTable.MainForm.IsHandleCreated) {
startsecondrunspace
}
Application.OpenForms() would be a method on an Application Class rather than the Form class. I am unsure if there is an instance of the Application class to even be able to use that method. If there was, I would imagine it should look something like this:
If ($ApplicationObject.OpenForms().OfType(Of $CommonHashTable.MainForm).Any) {
startsecondrunspace
}

Thanks you very much, I have created this function :
do {
RecordToLog -Message "Waiting..."
start-sleep -m 100
} until ($CommonHashTable.MainForm.IsHandleCreated)
startsecondrunspace
It's working.

Related

GUI update from another thread - WPF Powershell

I asked this question before, but I still am having issues. Really hoping I could have some assistance. all I need is to be able to add append text to my Rich TextBox from a background job. If I remove Start-Job -ScriptBlock { } it updates fine while on the GUI thread. What can I do in order to update my richtextbox from Start-Job?
Start-Job -ScriptBlock{
$richTextBox1.AppendText('++++++++++++++++')
$richTextBox1.Dispatcher.Invoke([action]{
$richTextBox1.AppendText('-------------')
},"Normal")
}
I'm not a massive user of Powershell within WPF however as I understand your post I think I can still help.
public void RunPowershell_Script()
{
Thread t = new Thread(new ThreadStart(
delegate
{
Start-Job -ScriptBlock{
this.Dispatcher.Invoke((Action)(() =>
{
//Any Element that you need to interact with variables
//or controls from the main application on the main
//dispatcher thread go here.
$richTextBox1.AppendText('++++++++++++++++')
$richTextBox1.AppendText('-------------')
}));
}
//Any final code can go here.
}
));
t.Start();
}
I believe this should work but you would have to have a play around. the above is the best way to access the main dispatcher thread in which the RTF box was originally created. Give it a try and let me know if it still doesn't work.

Create an attachment from MVCPortlet - serveResource

I need to export a .property file when user clicks on a hyperlink. I am using liferay portal 6.1.1. When user clicks on hyperlink, I am making a jquery.get() to the serveResource method which is inside the MyMVCPortlet class. Here the code that always writes the content to response(verified on fiddler) but is not creating a downloadable file.
resourceResponse.reset();
resourceResponse.setContentType("text/plain");
resourceResponse.setProperty("content-disposition", "attachment; filename=test.txt");
OutputStream out = resourceResponse.getPortletOutputStream();
try {
out.write("key=value".getBytes());
}
catch(IOException e){
e.printStackTrace();
}
finally {
out.close();//Also tried out.flush(); - dint help
}
Do I need to set something on resourceResponse after the write is complete?
I tried different options and got exhausted. The same code on plain java servlet works but not on liferay. Is there anything I am missing? Thanks in advance!
Try to use one of PortletResponseUtil.sendFile() instead of manual operations on portletOutputStream. Eg.
PortletResponseUtil.sendFile(resourceRequest, resourceResponse, "test.txt", "key=value".getBytes())

PowerShell with System Window Form Objects How to display command pane or confim boxes

I have a PowerShell Script that I like to run with a Visual Interface (GUI - with Windows Form elements) Everything is working so far but I have one big problem:
Is it possible to display the command pane from PowerShell on the created Windows Form?
For Example: In one part of my PowerShell Script I am running the following command:
Upgrade-SPContentDatabase DBName
This command requires to confirm some messages with "Yes/No" that will be normally displayed in the command pane from PowerShell... Can this be done over the Windows Form so that I can hide the PowerShell-Script Window in the background?
Or is there any other way to display it in a new window that comes up?
Screenshot:
Any Ideas?
A messagebox (GUI) is different from a prompt. As long as you run a script in powershell console(not ISE), prompts will show up in the console. (There may be a setting to make them GUI like in ISE). A workaround would be to try and disable the confirm-prompts in the script, and create a messagebox yourself.
Try the following in your button's click-handler:
$handler_button1_Click=
{
$n = [System.Windows.Forms.MessageBox]::Show("Are you sure?", "Confirm", [System.Windows.Forms.MessageBoxButtons]::YesNo)
if ($n -eq "Yes") {
#Ignore confirm dialogs with -Confirm:$false
Upgrade-SPContentDatabase DBName -Confirm:$false
}
}

In PowerShell Form.Show() does not work right, but Form.ShowDialog() does

I am trying to display an image via powershell. I made a script based on this forum post.
If I use ShowDialog() it works fine, except the powershell execution stops while the dialog is up. However, that is by design for a modal dialog. If I call Form.Show() in PowershellISE the form shows up, but freezes and cannot be moved or dismissed. Behavior is similar if I copy and past the code to a powershell console.
How do I make the dialog non-modal, and not freeze.
First Answer Why it appends.
In a Windows graphic program the thread which create a Window must loop in a message pump in order to redistribute (translate) messages coming from the user action to events in his Windows.
In a modal window, the modal code that handles the window display runs its own message pump loop and doesn't return until the window is closed. That's why the code after ShowDialog() won't execute until the window is closed.
Show(), just ask to show the Window, but if there is no pump loop to manage the messages coming from user action, it just freezes.
Second a simple way to have two threads
The CmdLet start-job use another thread from the pool allocated by Powershell so it makes the dialog non-modal, and it does not freeze.
function goForm
{
[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$file = (get-item 'C:\temp\jpb.png')
#$file = (get-item "c:\image.jpg")
$img = [System.Drawing.Image]::Fromfile($file);
# This tip from http://stackoverflow.com/questions/3358372/windows-forms-look-different-in-powershell-and-powershell-ise-why/3359274#3359274
[System.Windows.Forms.Application]::EnableVisualStyles();
$form = new-object Windows.Forms.Form
$form.Text = "Image Viewer"
$form.Width = $img.Size.Width;
$form.Height = $img.Size.Height;
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Width = $img.Size.Width;
$pictureBox.Height = $img.Size.Height;
$pictureBox.Image = $img;
$form.controls.add($pictureBox)
$form.Add_Shown( { $form.Activate() } )
$form.ShowDialog()
}
Clear-Host
start-job $function:goForm
$name = Read-Host "What is you name"
Write-Host "your name is $name"
There are ways to make this work, but nothing is worth spending five hours explaining on an open forum. There are other free, shrink-wrapped ways to do this on powershell. Most notably with the free WPF powershell toolkit: Show-UI at http://showui.codeplex.com/ (previously known as WPK and/or PowerBoots - they are merged now.)
If your goal is actually to not block the interactive console when an image is shown then you still can use the script as it is with ShowDialog but you should start it using, for example, Start-Job. Thus, the dialog is still modal but it blocks execution in another runspace. The main runspace still can be used for invoking other commands.
Caveats: 1) You should close all opened dialogs before closing the interactive console. 2) If you care, you should remove completed jobs yourself (when a dialog is closed a job that started it still exists).
I use a similar approach in my custom host and it works fine. I also tested it with the script from your link. I changed it slightly so that it is called show-image.ps1 and accepts a file path as a parameter.
This command shows an image and blocks the calling runspace:
show-image.ps1 C:\TEMP\_110513_055058\test.png
This command shows an image and does not block the calling runspace:
Start-Job { show-image.ps1 $args[0] } -ArgumentList C:\TEMP\_110513_055058\test.png
Building on #JPBlanc's anwer, this would also be possible (and faster) using a runspace.
Here's a basic example (the rest would basically remain the same)
$ps = [PowerShell]::Create()
[void]$ps.AddScript({
Add-Type -AssemblyName System.Windows.Forms
$form = [Windows.Forms.Form]::new()
$form.ShowDialog()
})
[void]$ps.BeginInvoke()

how to toggle between the running exe files using c#.net code on button click?

sir i want to know that how could i navigate/toggle between the different exe files using c#.net code on my .net application on the button click event..basically i want to do toggling between the current and previously opened running exe files..im able to run an exe file on my .net application but not able to provide an option for opening the previously running exe file...on the whole i want to provide a facility same as we are having in the OS windows that we could toggle between the applications on the taskbar menu
how could i do the same in the c#.net application using c# code.
thanks
Although not sure if I got you correctly but if you want to invoke a new exe on any event use:-
System.Diagnostics.ProcessStartInfo f = new System.Diagnostics.ProcessStartInfo("C:\\windows\\system32\\rundll32.exe",
"C:\\windows\\system32\\shimgvw.dll,ImageView_Fullscreen " +
fileName.TrimEnd (null));
try
{
// Pass the ProcessStartInfo object to the Start function.
System.Diagnostics.Process.Start (f);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine (ex.ToString ());
}

Resources