Powershell window form how to create OnTimeEvent()?

Filip 831 Reputation points
2021-06-28T20:11:48.31+00:00

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
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sam of Simple Samples 5,516 Reputation points
    2021-06-28T22:43:17.047+00:00

    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"  
    
    1 person found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. MotoX80 31,571 Reputation points
    2021-06-28T22:29:25.51+00:00

    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?

    2 people found this answer helpful.

  2. Sam of Simple Samples 5,516 Reputation points
    2021-06-28T21:16:02.833+00:00

    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()"  
    
    1 person found this answer helpful.
    0 comments No comments

  3. Filip 831 Reputation points
    2021-06-29T09:55:30.693+00:00

    Hello @Sam of Simple Samples & @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()"  
    
    0 comments No comments