question

Filip-2434 avatar image
0 Votes"
Filip-2434 asked MotoX80 commented

Powershell window form how to create OnTimeEvent()?

Hello.

I create simple windows form application in powershell

 Add-Type -AssemblyName System.Windows.Forms
 $Form = New-Object system.Windows.Forms.Form
 $Form.ShowDialog()


How to add this code to it?
In C# windows form application it look's like that:


 using System.Collections.Generic;
 using System.Runtime.InteropServices;
 using System.Timers;
 using System.Windows.Forms;
 using System;
    
 namespace WindowsFormsApp1Csh
 {
     public partial class Form1 : Form
     {
    
         private void Form1_Load(object sender, EventArgs e)
         {
             t = new System.Timers.Timer();
             t.Interval = 10;
             t.Elapsed += OnTimeEvent;
             t.Start();
         }
    
    
 private void OnTimeEvent(object sender, ElapsedEventArgs e)
         {
             Invoke(new Action(() =>
             {
                 Console.WriteLine("Hello");
             }));
    
         }
 }

Can i semohow create a public void metho or something like that?

Thanks for help.




windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SimpleSamples avatar image
1 Vote"
SimpleSamples answered SimpleSamples commented

Using Create a Windows Forms application from the command line you can create a Windows Forms program outside of Powershell using any plain text editor. Except you will need to find the C# compiler to execute it; for example it might be in:

 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe"



· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

That might be ok for something trivial, but if I'm writing anything with even a little bit of complexity, I want intellisense, drag and drop for controls, and the ability to set breakpoints and look at my variables.

0 Votes 0 ·

Yes compared to using Visual Studio the use of a plain editor (including Visual Studio Code for this) and command-line compiling would be inconvenient in many ways. Compared to using PowerShell it would be easier, such as the source code would be the same as if Visual Studio were used whereas with PowerShell the source code is different.

0 Votes 0 ·
SimpleSamples avatar image
1 Vote"
SimpleSamples answered SimpleSamples edited

The following works for me. Note that in Windows there is a difference between console applications and GUI (form) applications. You can use System.Timers.Timer in a console application. If you need a form then you need to use Visual Studio (not Visual Studio Code; the names sound alike but the tools are very different) to write the program(s). The following is an example in Timer Class.

 $code = @"
 using System;
 using System.Timers;
    
 namespace Example {
 public class Program
 {
    private static System.Timers.Timer aTimer;
       
    public static void Main()
    {
       SetTimer();
    
       Console.WriteLine("\nPress the Enter key to exit the application...\n");
       Console.WriteLine("The application started at {0:HH:mm:ss.fff}", DateTime.Now);
       Console.ReadLine();
       aTimer.Stop();
       aTimer.Dispose();
          
       Console.WriteLine("Terminating the application...");
    }
    
    private static void SetTimer()
    {
         // Create a timer with a two second interval.
         aTimer = new System.Timers.Timer(2000);
         // Hook up the Elapsed event for the timer. 
         aTimer.Elapsed += OnTimedEvent;
         aTimer.AutoReset = true;
         aTimer.Enabled = true;
     }
    
     private static void OnTimedEvent(Object source, ElapsedEventArgs e)
     {
         Console.WriteLine("The Elapsed event was raised at {0:HH:mm:ss.fff}",
                           e.SignalTime);
     }
 }
 }
 "@
    
 Add-Type -TypeDefinition $code -Language CSharp
 iex "[Example.Program]::Main()"


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
2 Votes"
MotoX80 answered MotoX80 commented

Have a look at this.

https://jrich523.wordpress.com/2011/06/13/creating-a-timer-event-in-powershell/

In C# windows form application it look's like that:

If you are trying to build a GUI application, and if you already have C# code snippets, then just write it in C#. As SimpleSamples noted, and as I replied to one of your earlier threads, use the free "Community" version of Visual Studio.

https://visualstudio.microsoft.com/vs/community/

Is there a reason that you trying to do this in Powershell?

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I agree it is better to use Visual Studio. The person trying to use Powershell will eventually realize it is not worth doing it.

0 Votes 0 ·

Hi @MotoX80 .
Yeah i know that better is it doing in Visual Studia (I also done it there), but the reason why I'm doing it there is that ,
I'm so much interester and currius of power that have powershell comandline.
Also I tried to train and practise my programin skills with doing some funny stuffs :).

0 Votes 0 ·

@Filip-2434,

I understand. I am retired but I still want to keep up my programming skills. That's why I participate in these forums and try to answer other users questions. There is always something to learn.

1 Vote 1 ·
Filip-2434 avatar image
0 Votes"
Filip-2434 answered Filip-2434 edited

Hello @SimpleSamples & @MotoX80 .
Thanks for answars, it help me.
This is my final code.

 Add-Type -TypeDefinition @"
 using System;
 using System.ComponentModel;
 using System.Drawing;
 using System.Windows.Forms;
 using System.Timers;
    
 namespace Test
 {
     public class Form1 : Form
     {
         public Button button1;
     System.Timers.Timer t;
         public Form1()
         {
             button1 = new Button();
             button1.Size = new Size(40, 40);
             button1.Location = new Point(30, 30);
             button1.Text = "Click me";
             this.Controls.Add(button1);
             button1.Click += new EventHandler(button1_Click);
             Form1_Load();
    
         }
        
         private void Form1_Load()
         {
             t = new System.Timers.Timer();
             t.Interval = 1000;
             t.Elapsed += OnTimeEvent;
             t.Start();
         }
    
         private void button1_Click(object sender, EventArgs e)
         {
             MessageBox.Show("Hello World");
         }
    
       private void OnTimeEvent(object sender, ElapsedEventArgs e)
         {
             Invoke(new Action(() =>
             {
         Console.WriteLine("Hello world");
         }));
     }
    
    
         [STAThread]
         public static void Main()
         {
             Application.EnableVisualStyles();
             Application.Run(new Form1());
         }
     }
 }
 "@ -ReferencedAssemblies System.Windows.Forms, System.Drawing

 iex "[Test.Form1]::Main()"



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.