Automating a WinForms/WPF application

55 views
Skip to first unread message

Yves

unread,
Feb 27, 2012, 2:15:03 PM2/27/12
to CS-Script
Hi,

I'd like to automate a WinForms or WPF application through C#
scripting. I.e. my GUI exposes some functionality to the user and a
script shall use these functions to do automated tasks. The GUI has
TextBoxes, CheckBoxes, Buttons and such and it displays data and
accepts interactive user input. The application is about controlling
other services over a network connection, so new data shows up every
now and then and the user input is sent back.

I managed to integrate CS-Script to the extent that I can run a script
with a static method which can access and manipulate my GUI. But there
is a problem with that: The script runs on the same thread as the GUI.
When the script waits for something, the GUI is blocked and nothing
will ever happen.

There seem to be numerous examples that demonstrate using CS-Script in
a plugin-like way, where the script does a job and its result is used
by the host application, waiting for it. My case is exactly the
opposite. The host does jobs and the script shall invoke them in some
order, at some times.

So I need a separate thread for my script. But then, I cannot simply
access my GUI anymore because GUIs are not threadsafe. The
MarshalByRef examples work in the wrong direction. And I'm not so
familiar with remoting so that's a bit of a problem for me now.

What's the best way to make it work? Just take the most simple
example, do my own threading and GUI invoking? Or is there some easier
solution?

Oleg Shilo

unread,
Feb 29, 2012, 1:07:31 AM2/29/12
to CS-Script
The thread-safety problem you are experiencing is not really related
to the scripting as such but to the threading model WinForms/WPF is
using.

I have prepared a straight forward example for you that demonstrates
how to deal with the problem. Note the "fix" is not in the script but
in the application you are trying to automate. This is where tha
marshaling needs to be done.

You can find the sample here: http://dl.dropbox.com/u/2192462/Support/Automation.7z

Cheers,
Oleg
Reply all
Reply to author
Forward
0 new messages