Steven's profileLunatic ExperimentsPhotosBlogLists Tools Help

Blog


    May 27

    Of Powershell And Compiled Events

    After writing my last post I realized that I didn't have a script that would simplify the process of generating event handlers down to a single line task.

    I quickly wrote a script that wraps C# method code into a delegate. The script allows delegates of any signature to be created. The result has four parameters, three of witch have default values, and only 17 lines of actual code. This script should be fairly easy to use.

    The default values are set to use the EventHandler delegate. I chose this type of delegate because Powershell script blocks can be cast to EventHandler. I wanted to be able to easily generate a compiled delegate and be able to combine it with a script block.

    Get-CDelegate.ps1

    Here is an example of its use.

    PS> $handler = Get-CDelegate 'Console.WriteLine("Hello world!");'
    PS> $handler.Invoke($null,$null)
    Hello world!

    Simple! Right? Think of the compiled delegate as if it were a script block that runs really fast.

    You can also make delegates of different types.

    PS> $callback = Get-CDelegate 'Console.WriteLine("Hello world!");' -DelegateType AsyncCallback -Signature void, IAsyncResult

    Here's the cool part. Combine a script block with an event handler to make a new event hander.

    PS> $eh1 = get-cdelegate 'Console.WriteLine("Compiled EventHandler!");'
    PS> $eh2 = [eventhandler]{write-host 'ScriptBlock!'} PS> $eh3 = [delegate]::combine($eh1, $eh2) PS> $eh3.invoke($null,$null) Compiled EventHandler! ScriptBlock!

    There is also a using parameter. Add any namespaces that you want to use there. The default is 'System'.

    Now, take it, have it, play with it, enjoy it, and remember, the key making this useful is to keep in mind that the result of this script is a reusable function that can do something that is hard much faster than an ordinary script block. Now go find an interesting use for this and post about it!

    Update

    I expanded the script so that the delegate can receive an object from the script that made it and also be able to remember past invocations. Script blocks store all their context information as variables, but that's not so easy in this case. Here there is no guarantee that there is even a Runspace associated with the thread that invokes the delegate. Now you can provide an initialization object, optional initialization code, and define the types for any values that should remain present between invocations.

    PS> $eh = get-cdelegate 'context0++; Console.WriteLine(context0);' -contexttypes int -initobject 50 -initcode 'context0 = (int)init.BaseObject;'
    PS> $eh.invoke($null,$null)
    51
    PS> $eh.invoke($null,$null)
    52
    PS> $eh.invoke($null,$null)
    53
    PS> $eh.invoke($null,$null)
    54
    

    May 26

    Powershell and Events in WPF

    I have seen this a few times in my Google referrals. People want to know how to how to add events to PresentationFramework controls in Powershell.
    I'm sorry to tell you this, but, as of Powershell v1.0, you can't use script blocks generated in Powershell as event handlers in any STA style user interface, including WPF. This has to do with how Powershell currently handles threading. That's why I had to make three really long scripts just to show a single window.
    If you really want to be able to add event handlers to WPF controls have a look at my New-CAssembly script. That will make it easier to include compiled code, such as event handlers, into a Powershell script. Then, look at my Library-PresentationInterface script. That will help you pack a custom user interface into a script as XAML.
    Alternatively you may look at a few of Team Powershell's recent posts about controlling WPF through Powershell v2.0 CTP 2. They added an STA flag to CTP 2. This helps to resolve some of the threading issues in regards to controlling STA style user interfaces.

    May 25

    About the "Anti-Counterfeiting Trade Agreement" proposal

    A document titled "Anti-Counterfeiting Trade Agreement" has just come to my attention. I have never been any more infuriated since I read one particular document that suggested that the department of Homeland Security should have to report every instance of copyright infringement they find to the RIAA!

    I feel as if I had just watched one of those NWO conspiracy videos. I hope that the many ambiguities in this proposal can be removed because the truth is that all things will be at sometime abused and I can only see an end to the "innovation and creativity" that this document claims to protect when this document becomes subject to abuse.
    The overall spirit of the document is counter piracy. However, what is being ignored, and has been ignored since the founding of the RIAA, is that a person that intentionally attain a product illegally originally intended to not attain the product legally. That is to say that a person has proven that they can not be considered a potential source of revenue once they have proven to a pirate. It can be further stated that a pirate, in the purely intellectual property sence and only in the intellectual property sence, has technically done no harm as there has been no loss. That does not go to say that a pirate can not cause harm. e.g. Distributing faulty medical equipment(a fake copy of such equipment) will definitely cause harm, however that goes beyond piracy. In short, piracy itself, that is document suggests to place so much resource against, is little more than an annoyance.

    There is one line that I, even as I am an IP rights holder, particularly do not like. It suggests that an institution be founded with the right to persue legal action against anyone it sees as having infringed on any IP right prior to and without the consent of the IP right holder. Can someone explain to me how any such institution is to know who is authorized to distribute any product without first consulting the rights holder?

    I like the idea that my copyrights could be protected on a global scale. I don't like anything else about this proposal.
    This proposal needs to be rewritten! I don't want anyone harassing my, or anyone else's, customers!
    May 09

    Visual Design in JavaScript

    I would just like to say this is really cool. It makes me want to start focusing on JavaScript again.
    Go ahead. Click the link and check out the examples.

    The script Processing.js is actually a go between a visual design language called Processing and the HTML canvas element.