Friday, November 04, 2005

Prerequisites for WinForms development on OS X

As with most large-scale applications, the application I’m attempting to move to OS X is comprised of many individual DLLs. This architecture should be a benefit to me as I will be able to isolate and certify each individual DLL on OS X before attempting to run the entire program.

I started by creating a test harness for the simplest DLL within my application which contains my standard exception handler, event logging, and other low-level utilities. This DLL is also used by all other DLLs within my system.

The first problem I encountered was how to capture debug information so I can figure out what is going on under the covers. Since I’m doing all WinForms development I don’t have the benefit of a terminal to debug out information to. There may be a debug output viewer available for OS X, but I’m not aware of one. My solution was to use the standard .NET/Mono debug out statement and implement a custom trace listener.

Creating a custom trace listener in .NET is a piece of cake as shown below:

//BEGIN CLASS CODE

public class DebugOutput : System.Diagnostics.TraceListener
{
public DebugOutput()
{

}

public delegate void outputDebugString(string Text);
public event outputDebugString OutputDebugString;

public override void Write(string info)
{
try
{
OutputDebugString(info);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

public override void WriteLine(string info)
{
try
{
OutputDebugString(info);
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}

//END CLASS CODE

The next step was to use the custom trace listener in my test harness.

Step 1: Create the event handler.
private void OutputDebugString(string Text)
{
try
{
lblDebugOutput.Text = lblDebugOutput.Text +
"NEW STATEMENT:" +
System.Environment.NewLine + Text + System.Environment.NewLine + System.Environment.NewLine;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

Step 2: Initialize the custom trace listener and the event handler.
private DebugOutput m_objDebugOutput = null;

private void frmTestHarness_Load(object sender, System.EventArgs e)
{
m_objDebugOutput = new DebugOutput();
m_objDebugOutput.OutputDebugString += new
DebugOutput.outputDebugString(OutputDebugString);
System.Diagnostics.Debug.Listeners.Add(m_objDebugOutput);
}



Now I’m able to use debug statements within my application and see them on screen.

VERY IMPORTANT: When I started this, I attempted to use a TextBox to display the debug output. For some reason, the act of setting text in my TextBox caused my application to immediately crash. Next I tried to send UPD packets to another computer as an alternate way of viewing debug output. This also didn’t work for me. Finally I ended up using a Label. A Label works fine, but I’m going to need to go back later and figure out what the problem is with TextBoxe controls and the UDP client as both of these are used within my application.

I’m now in the process of verifying that all functions act exactly the same way on both Win32 and OS X.

More to come.

-d

0 Comments:

Post a Comment

<< Home