SyntaxHighlighter

Wednesday, August 20, 2008

AJAX ProgressUpdate issue

I had an issue with the UpdatePanel and ProgressUpdate AJAX extensions controls in ASP.NET.

The following error occurred in the Javascript:
if (this._dynamicLayout) this.get_element().style.display = 'block';

I solved the problem by moving my ProgressUpdate control out of the UpdatePanel's tags. I guess I was looking for trouble when I did that.

Having the ProgressUpdate within the UpdatePanel work in certain instances, but I also had another UpdatePanel in the same UserControl, so it got a bit hairy.

I'd like to verify this, but as always, the deadline looms.

Wednesday, August 13, 2008

Constraint cannot be special class 'object'

I was busy creating a generic method to convert a List to a DataTable when I received the error:
Constraint cannot be special class 'object'
on this statement:
public DataTable ListToDataTable(IList list) where T : Object

Blegh... why would this be? Anyhow, I did a workaround by changing the statement to the following:
public DataTable ListToDataTable(T list) where T : IList

A bit annoying, since now one has to send a List and can't simply pass a List.

Another workaround is to use a blank interface, with the statement as:
public DataTable ListToDataTable(IList list) where T : IBlank

Now one can send List if MyClass 'implements' the IBlank interface.

I'm not really too happy with any of these workarounds. I see no reason why the Object class is "too special" to be used as a generic placeholder.

Oh, and merely for interest sake. Here is the generic List to DataTable converter method:
public DataTable ListToDataTable(List list) where T : IBlank
{
DataTable dataTable = new DataTable();

Type type = typeof(T);
PropertyInfo[] properties = type.GetProperties();

// Create the columns
foreach (PropertyInfo property in properties)
dataTable.Columns.Add(property.Name, property.PropertyType);

// Populate the rows
foreach (T obj in list)
{
DataRow newRow = dataTable.NewRow();

foreach (PropertyInfo property in properties)
newRow[property.Name] = property.GetValue(obj, null);

dataTable.Rows.Add(newRow);
}

return dataTable;
}

Wednesday, July 30, 2008

Python: Multi-dimensional dynamic arrays

Another very poorly documented topic is how to create dynamic arrays of type array. Every second website will show you how to create static 2D arrays, which is pretty much useless for 90% of the practical applications out there.

I 'solved' it to a degree by creating a list of array items, but this is still not ideal. I would like to use a pure multi-deminsional array of type array.

Here's how I did it:
multi_arr = []

for i in range(some_range):
arr = array('f') # array of type float

# ... populate arr ...

multi_arr.append(arr)

Julian Day in Python

I found very little documentation on obtaining the Juilian day with the time module in Python. So this is how it's done:
import time

print time.strftime("%j")

As easy as that!

Monday, July 28, 2008

Linux: shutdown: you must be root to do that!

In order to use the console commands shutdown, poweroff, halt or reboot, one has to be a super user. One might see messages such as:
shutdown: you must be root to do that!
poweroff: must be superuser.
reboot: must be superuser.
halt: must be superuser.
In some cases the user can simply be added to the wheel group. This should give the user enough privileges to shut the machine down. A more reliable approach is to make use of Sudo, which was specifically designed to allow ordinary users to execute certain super-user-only commands.

The following section will explain how to setup sudo to allow other users to the machine down through the command line under the Gentoo distro.

Install Sudo
Login as su:
su -
Emerge Sudo:
emerge app-admin/sudo

Configure Sudo
Run visudo:
visudo
Enter the following:
# Runas alias specification
YourUserName ALL=(root) NOPASSWD: /sbin/reboot
YourUserName ALL=(root) NOPASSWD: /sbin/halt
YourUserName ALL=(root) NOPASSWD: /sbin/poweroff
YourUserName ALL=(root) NOPASSWD: /sbin/shutdown
Replace YourUserName with the user name which requires shutdown privileges.

See this guide on how to create a group which allows all its users to shut the machine down.

Sunday, July 13, 2008

Named pipes issue: WaitForConnection hangs around indefinitely

I recently created an IPC client and server based on the newish NamedPipeServerStream and NamedPipeClientStream classes in .NET 3.x.

It worked great, but when trying to stop the server, the main thread would hang aound waiting for the WaitForConnection() statement to return. I tried to kill the thread in a number of ways, but to no avail.

In the end, I created an easy workaround by doing a "dummy" client connection to the server. This will cause WaitForConnection() method to continue and the thread can then be stopped gracefully.

Friday, July 11, 2008

Named pipes issue: System.UnauthorizedAccessException: Access to the path is denied.

After yesterday's triumphant discovery I had a nightmare of a day trying to figure out why my client couldn't connect to the server. Time and time again, the following exception was raised:

System.UnauthorizedAccessException: Access to the path is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.Pipes.NamedPipeClientStream.Connect(Int32 timeout)
at System.IO.Pipes.NamedPipeClientStream.Connect()
And in the debug output window:
A first chance exception of type 'System.UnauthorizedAccessException' occurred in System.Core.dll

My best googling skills couldn't find an answer for this one and I ended up trying to change the permission for the NamedPipeServerStream and venturing down the wrong path by changing the PipeAuditRule and PipeAccessRule settings.

In the end turned out my PipeDirection had to be set to InOut, even though I was only streaming information from the Server, and not receiving information.

Here's the winning line of code:

NamedPipeServerStream Pipe = new NamedPipeServerStream(PipeName, PipeDirection.InOut, 1,
PipeTransmissionMode.Message, PipeOptions.None);