# Color Of Code

Cpanel

## C# Detect Processes Demanding user input

User Rating:  / 0

## Problem description

Some time ago, I asked a question on stackoverflow.com. How to programmatically (C#) determine, if ANOTHER external application (native, java, .NET or whatever...) is currently demanding user input? Could this be done fully in Managed code? What would be the implementation of

static Boolean IsDemandingUserInput(String processName)


By demanding user input I mean, when an application asks the user to enter some data or quit an error message (Modal dialogs) and is not able to perform its normal tasks anymore. A drawing application that is waiting for the user to draw something is not meant here. The question seems to interest a lot of people but so far I got no answer that satisfied my needs, despide some great and competent people there.

As the problems I want to solve are more of practical and not theoretical nature, I started to implement a solution. This one at least detects many of the situations inside a running application I want to be informed about.

## Solution (partial)

I worked out a solution that seems to work, please notify me in case of problems with this code so I also gain benefit of improvements. It works for Excel as far as I tested. The only issue I dislike is that I had to use unmanaged calls. It also handles the case when an application is based on a dialog like for MFC, derived from CDialog. Unfortunately I could not find a pure managed solution. Do you have better ideas?

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace Util
{
public class ModalChecker
{
public static Boolean IsWaitingForUserInput(String processName)
{
Process[] processes = Process.GetProcessesByName(processName);
if (processes.Length == 0)
throw new Exception("No process found matching the search criteria");
if (processes.Length > 1)
throw new Exception("More than one process found matching the search criteria");
ModalChecker checker = new ModalChecker(processes[0]);
return checker.WaitingForUserInput;
}

#region Native Windows Stuff
private const int WS_EX_DLGMODALFRAME = 0x00000001;
private const int GWL_EXSTYLE = (-20);
private delegate int EnumWindowsProc(IntPtr hWnd, int lParam);
[DllImport("user32")]
private extern static int EnumWindows(EnumWindowsProc lpEnumFunc, int lParam);
[DllImport("user32", CharSet = CharSet.Auto)]
private extern static uint GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32")]
private extern static uint GetWindowThreadProcessId(IntPtr hWnd, out IntPtr lpdwProcessId);
#endregion

// The process we want the info from
private Process _process;
private Boolean _waiting;

private ModalChecker(Process process)
{
_process = process;
_waiting = false; //default
}

private Boolean WaitingForUserInput
{
get
{
EnumWindows(new EnumWindowsProc(this.WindowEnum), 0);
return _waiting;
}
}

private int WindowEnum(IntPtr hWnd, int lParam)
{
if (hWnd == _process.MainWindowHandle)
return 1;
IntPtr processId;
if (processId.ToInt32() != _process.Id)
return 1;
uint style = GetWindowLong(hWnd, GWL_EXSTYLE);
if ((style & WS_EX_DLGMODALFRAME) != 0)
{
_waiting = true;
return 0; // stop searching further
}
return 1;
}
}
}


## References

• Comments and questions please to me: This email address is being protected from spambots. You need JavaScript enabled to view it.
• The problem as question on Stack Overflow

## Subversion Repository Backup

User Rating:  / 0

## Introduction

The source code is - as the name already tells - the real valuable data for software development. Therefore a proper backup of this important data is absolutely required.

Backups shall provide protection against hardware failures (Hard Disk crash, ...) but ideally also protect against erroneous software (defects in the version control software itself, DB corruption, ...). Usually problems appear slightly resulting in a corrupted DB. The danger of a slowly degrading system is that you might backup an already corrupted DB. So, if you are lucky you will have a full crash and a healthy backup. If your backup got already corrupted as well, on restoration, you will face the same problems as with the original DB.

This article will provide hints on possible backup solutions for subversion repositories to help choosing an appropriate one.

## Subversion

Subversion's repository structure is concentrated in one directory (conf, dav, db, hooks, locks). Each repository holds the bare source, in the db directory. The structure of this db directory varies slightly for subversion from version to version. The data is stored in form of delta files containing the so-called changesets (consistent set of changes across several files). In the versions I looked at, the properties are also stored in separate files. But the db directory also contains extra data, like specific scripts (hook scripts) or the repository configuration (in the conf directory). Beside this, transactional data (valid during a commit for example) is also stored there until the action can be made all at once (atomic commit feature from subversion). The nature of a version control system of beeing additive qualifies it well for incremental backup procedures.

### Backup

For backing up the repositories, you have following options (see the svn book for details regarding on how to perform them):

1. A naive approach using standard backup software (rsync, ...)
4. svnsync (can be done remotely, without file access to the repository being sync'ed)
5. a git svn clone (possible but drawbacks/advantages to be analyzed, feel free to give me feedback)

The solutions differ in:

• (a) if the backup can be done online or if the server has to be stopped during the backup
• the amount of data they back-up (some solutions do not copy all the repository information)
• (b) allowing incremental backups or not. Hotcopy only allows full backups for example.
• (c) Can the hook scripts or the configuration files also be backed up with this method?
• (d) Can the backup be directly used as a read-only fallback solution for the original server. In the case of git-svn the backup can be cloned and used with a git client only.
• (e) restoration time, how quick you can go back to normal work. For transferring the git repository into an svn repository there is quite some work to perform: dump out the repository and load it into subversion. That makes the git-svn approach even slower than the dump solution regarding the time needed to restore the repository.
• (f) independence of the back-end tool or format. This is an advantage to deal with eventual defects in the tool itself. If the repository data is faulty then the data backed-up will also contain the defects. In the case of a text dump the resulting backup is independent from the binary format of a specific subversion version. But this has a cost: the time to restore is high.
• (g) remote: if the backup can be performed remotely (with no direct access to the filesystem holding the repositories). Actually from the presented methods only svnsync and git-svn allow this mode of operation.
 1) naive 2) dump 3) hotcopy 4) sync 5) git-svn a) online - yes yes yes yes b) incremental yes yes - yes yes c1) hook files yes - yes - - c2) config files yes - yes - - d) backup RO yes - yes yes* yes (git) e) restore time File copy Restore (long) File copy File copy* dump -> load f) back-end independence - yes - - yes g) remote - - - yes yes

* Take care of the uuid of the repository it can be changed manually to match the original one in the db directory. In case of mismatching uuids a relocate to the backup repository will not succeed.

Another pitfall is that in the case of svnsync and git-svn, the user performing the backup needs an account with full access to all paths to the source repository. Failing to do that will result into an incomplete backup.

### Restoration

The restoration of a backup is a procedure that is quite time consuming in the case of dump files. These have to be loaded with "svnadmin load". Rebuilding the repository can then take ages depending on the amount of data to restore. Until then you will not be able to work. The hotcopy can be re-used as it is, as well as the self made copy using a standard backup tool. You will need to copy back the files again. The sync solution will need adapting the uuid and replacing the hook files (which you will have to backup manually) but in principle it is also quite easy.

## Recommendation

Considering all these aspects I would recommend using svnsync to create a copy that can be updated incrementally at any time. Take care of copying the hook scripts and config files separately as well as the uuid. (To operate svnsync you will need to allow the pre-revprop-change hook temporarily)

I recommend against the hotcopy unless you have small repositories. In the case of several GB of data, the unability of hotcopy to operate incrementally is a KO criteria (scales badly with the repository size).

Beside this, a regular textual dump with "svnadmin dump" of the repositories is advised for dealing with the scenario where the normal backup is unusable. This is maybe quite paranoid, but it's there as a fallback in this worst case scenario.

User Rating:  / 0

# Debugging inside a generated .NET assembly

.NET assemblies can be generated or compiled from a string directly in memory. This is a great thing as it enables you to make advanced scripting available to your users. They have the full features of C# or any other .NET language and you can bind the generated assembly into your application. This is well known, but there are some difficulties I encountered regarding debugging.

How can one debug the code generated while debugging the generating program? The methods or classes are not known to the application running the script. Using a MethodInvoker lets the code run but you cannot step into it.

## Sample code

-< work in progress >-

See my question on stack overflow.

## Preparation

Some requirements must be met to enable debugging inside such generated assemblies. First of all, the symbols must be available, as well as the source code.

## Calling Break

One possible solution involves the introduction of System.Diagnostics.Debug.Break(); into the script. This works well, the debugger stops at the place where the break was put. But this method is invasive: you have to modify the code of the script to make it work.

## Using interfaces

Another way is to agree about a contract, an interface that both application and script know. One instanciated object that implements the interface can be manipulated by using the interface methods. By stepping into a call to one method, the debugger is able to resolve the source code and display the next line of the generated assembly. You can debug as usual having full debugging capabilities.

## Visual Studio 2010

To my surprise VS 2010 handles the debugging in assemblies generated in memory cleanly. You have the full debugging options.

## Math-JAX

User Rating:  / 0

Great piece of software to typeset mathematical expressions nicely inside the Web: http://www.mathjax.org/

When $a \ne 0$, there are two solutions to $$ax^2 + bx + c = 0$$ and they are $$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$