- Cannot call BeginInvoke on a delegate in WIndows CE version of .NET
- Open Net Compact framework Library
August 19, 2006
WIndows CE Notes.Links
Initializing A ComboBox In CE.NET With The Values of An Enum
SomeEnum status;
int offset = 0;
bool defined = false;
do
{
status = (SomeEnum)offset++;
defined = Enum.IsDefined(typeof(SomeEnum), status.ToString());
if (defined)
{
comboBox.Items.Add(status);
}
} while (IsDefined);
comboBox.SelectedIndex = 0;
Using A ListView
To get a nice excel type view of the list set the properties 'View=Details' and 'GridLines=True' or put the following code in the designer
//this.listView.Anchor = //Dont forget to set the anchors appropriately this.listView.AllowColumnReorder = true; this.listView.View = System.Windows.Forms.View.Details; this.listView.GridLines = true; // Select highlights a full row this.listView.FullRowSelect = true; // Select a single row only this.listView.MultiSelect = false; // Keep selected rows highlited even when the control does not have the focus this.listView.HideDelection = false;Adding items to the ListView
private const int NAME_COLUMN = 0;
private const int ID_COLUMN = 1;
void PopulateListView()
{
// DONT call "listView.Clear()" or you'll lose all your
// column data and then wonder why you table is empty!
listView.Items.Clear();
// Add the item to the ListView
string[] fields = new string[]
{
"column1string",
"column2string",
...
};
OR
fields[NAME_COLUMN] = xxx.Name;
fields[ID_COLUMN] = xxx.Id.ToString();
...
ListViewItem lvItem = new ListViewItem(fields);
// Insert at the top of the listview control
listView.Items.Insert(0, lvItem);
// Append to the end of the ListView control
listView.Items.Add(lvItem);
// with custom listviewitems
MyListViewItem li = new MyListViewItem(...)
listView.Items.Add(li);
}
To update a column:
private void UpdateIdColumn(int id)
{
listView.Items[m_SelectedIndex].SubItems[ID_COLUMN].Text = id.ToString();
}
Optionally define a custom list view item which holds a reference to one of your types XXX
internal class MyListViewItem : ListViewItem
{
internal MyListViewItem(XXX xxx)
: base(new string[] {
xxx.Name,
xxx.Description,
})
{
XXX = xxx;
}
internal XXX XXX { get; set; }
}
private void listView_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView.SelectedItems.Count == 0)
{
// Are there any controls need enabling/
// disabling when the nothing is selected
button.Enabled = false;
return;
}
// Are there any controls need enabling/
// disabling when the nothing is selected
otherCtrl.Enabled = true;
foreach (ListViewItem lvi in lvParts.SelectedItems)
{
((XXX)lvi.Tag).DoSomething();
// or
MyCustomListItem li = lvi as MyCustomListItem;
if (li != null)
{
li.XXX.DoSomething();
}
break;
}
}
Dont forget to add a method to resize the columns:
private void FitColumnsToListView()
{
int extra = ListViewSpareColumnSpace(lvParts);
if (extra > 0)
{
lvParts.Columns[0].Width += extra;
}
}
private int ListViewSpareColumnSpace(ListView lv)
{
if (lv.Columns.Count == 0)
return 0;
int width = lv.Size.Width;
int usedWidth = 0;
// 2 pixels for the divider lines
foreach (ColumnHeader ch in lv.Columns)
{
usedWidth += ch.Width + 2;
}
int extra = width - usedWidth;
return extra;
}
To disable selection of listview items, handle the ItemSelectionChanged event on the listView:
private void listView_ItemSelectionChanged(object sender,
ListViewItemSelectionChangedEventArgs e)
{
if (e.IsSelected)
e.Item.Selected = false;
}
Use Reflection To Set A Value On A Field
Setting a field on an object by reflection
private static void SetFieldViaReflection(
object obj,
string fieldName,
object value)
{
Type InstanceType = Instance.GetType();
FieldInfo fi = InstanceType.GetField(fieldName,
BindingFlags.NonPublic | BindingFlags.Instance);
if (fi != null)
{
fi.SetValue(obj, value);
}
}
SetFieldByReflection(object, "m_field", somevalue as object);
June 6, 2006
Boxing with C#
Theres nothing like learning by trying. Here is some code that I wrote that explicitly demonstrates boxing. I found in MSDN a little boxing test so I copied the balls of it (hope thats not illegal or anything) to here. The original article can be found through a quick google search. It is a good article on boxing.
June 4, 2006
Thread Pattern Starter Class
Background thread class as a starter for a threading class.
class WorkerThread
{
public WorkerThread()
{
StartThread();
}
~WorkerThread()
{
Dispose(false);
}
#region IDisposable Members
private bool m_disposed = false;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!m_disposed)
{
if (disposing)
{
// Dispose managed resources
}
// Dispose unmanaged resources
EndThread();
}
m_disposed = true;
}
#endregion
#region Private Threading Related
/// <summary>
/// Actual threa object
/// </summary>
private Thread m_Thread = null;
/// <summary>
/// Give the thread a meaningful threadId
/// </summary>
private string threadId = "MyThreadName";
/// <summary>
/// Time to wait for the thread to finish
/// when merging the thread
/// back to the main thread
/// </summary>
private uint waitForEndThread = 5000; // 5 seconds
/// <summary>
/// How long to pause for at the end of each iteration
/// </summary>
private int m_ThreadSleepPeriod = 200;
/// <summary>
/// We use the m_PauseEvent to control the sleep period
/// of the thread. The m_PauseEvent
/// sleeps for the given period but we can force it to
/// wake up early by calling
/// Set on it.
/// </summary>
private AutoResetEvent m_PauseEvent = new AutoResetEvent(false);
/// <summary>
/// This does not need special protected access.
/// It is set to false before the thread is
/// started and can only get set to true when
/// the thread thread has begun.
/// </summary>
private bool m_Stop = false;
/// <summary>
/// Start the thread running
/// </summary>
private void StartThread()
{
if (m_Thread != null)
{
EndThread();
}
if (m_Thread == null)
{
if (m_PauseEvent == null)
{
m_PauseEvent = new AutoResetEvent(false);
}
m_Stop = false;
m_Thread = new Thread(new ThreadStart(
this.ThreadRoutine));
m_Thread.IsBackground = true;
m_Thread.Start();
m_Thread.Name = threadId;
Thread.Sleep(0);
}
}
/// <summary>
/// EndThread to the thread to terminate it
/// </summary>
private void EndThread()
{
m_Stop = true;
if (m_Thread != null)
{
if (m_PauseEvent != null)
{
m_PauseEvent.Set();
}
try
{
if (m_Thread.IsAlive)
{
System.Diagnostics.Debug.WriteLine(
"WorkerThread.EndThread(): EndThreading this thread \'"+
m_Thread.Name + "\' to \'" + Thread.CurrentThread.Name + "\'");
// Wait X seconds for the thread to finish
if (!m_Thread.Join((int)waitForEndThread))
{
System.Diagnostics.Debug.WriteLine(
"WorkerThread.EndThread(): EndThread() failed, so ABORTing thread\'" +
m_Thread.Name + "\'");
m_Thread.Abort(); // otherwise abort
}
else
{
System.Diagnostics.Debug.WriteLine(
"WorkerThread.EndThread(): EndThread() succeeded, Thread \'"+
m_Thread.Name + "\' is terminated");
}
}
else
{
System.Diagnostics.Debug.WriteLine(
"WorkerThread.EndThread(): Thread \'" +
m_Thread.Name + "\' is not 'Alive' so EndThread() not invoked");
}
}
catch (System.Threading.ThreadStateException ex)
{
// Swallow any ThreadStateExceptions
System.Diagnostics.Debug.WriteLine(
"WorkerThread.EndThread(): ThreadStateException caught in thread \'" +
m_Thread.Name + "\'" + ex.ToString());
}
m_Thread = null;
if (m_PauseEvent != null)
{
m_PauseEvent.Close();
m_PauseEvent = null;
}
}
}
/// <summary>
/// The routine executed within the new thread
/// </summary>
private void ThreadRoutine()
{
string thrdName = m_Thread.Name;
System.Diagnostics.Debug.WriteLine("Thread \'" +
thrdName + "\' thread routine is started");
try
{
while (m_Stop != true)
{
ThreadWork();
if ((m_Stop != true) &&
(m_ThreadSleepPeriod >= 0))
{
m_PauseEvent.WaitOne(m_ThreadSleepPeriod,
false);
}
}
}
catch (Exception ex)
{
m_Stop = true;
//if (log != null)
System.Diagnostics.Debug.WriteLine(
"FATAL exception caught in thread routine \'" +
thrdName.ToString() + "\' Thread will be terminated" + ex.ToString());
}
System.Diagnostics.Debug.WriteLine("Thread \'" + thrdName +
"\' end of thread routine reached");
}
private void ThreadWork()
{
// Do something
}
#endregion
}
Windows Service Pattern
Use this template as an example for creating a service.
Note that XXX is the service implementation class, this class just integrates that service object into windows services.
Note that XXX is the service implementation class, this class just integrates that service object into windows services.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Runtime.Remoting;
using Logging = log4net;
namespace XXX.service
{
/// <summary>
/// Main service class as created by the code generator.
/// </summary>
public class XXXService
{
#region Logging
private static readonly Logging.ILog log =
Logging.LogManager.GetLogger(typeof(XXXService));
#endregion Logging
/// <summary>
///
/// </summary>
public static readonly string ServiceDisplayName =
"XXXService";
#region Private Fields
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components
= null;
/// <summary>
/// Did the service start up successfully
/// </summary>
private bool startSuccesful = false;
#endregion Private Fields
#region Constructor
/// <summary>
/// XXXService constructor.
/// </summary>
public XXXService()
{
// This call is required by the Windows.Forms
// Component Designer.
InitializeComponent();
}
#endregion Constructor
#region Main/Initialise
// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// More than one user Service may run within the
// same process. To add another service to this
// process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = new
// System.ServiceProcess.ServiceBase[] {
// new XXXService(),
// new MySecondUserService() };
//
ServicesToRun = new System.ServiceProcess.ServiceBase[]
{ new XXXService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = ServiceDisplayName;
}
#endregion Main/Initialise
#region IDispose Implementation
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#endregion IDispose Implementation
#region Service Methods
private bool IsDebugParameter(string arg)
{
var param = arg.ToUpperInvariant();
return (param == "-DEBUG") || (param == "/DEBUG");
}
/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
// Allow us to be run in a debugger - for
// development time support.
if ((args.Length == 1) && IsDebugParameter(args[0]))
Debugger.Launch();
startSuccesful = false;
string fileName = string.Empty;
try
{
log.Info("OnStart: Starting \'" + ServiceDisplayName<br> + "\'");
System.IO.FileInfo fi = new System.IO.FileInfo(
System.Windows.Forms.Application.ExecutablePath);
System.Environment.CurrentDirectory =
fi.Directory.ToString();
log.Info("Setting current directory to "
+ System.Environment.CurrentDirectory);
fileName = System.Windows.Forms.Application.ExecutablePath
+ ".config";
log.Info("Loading configuration from " + fileName);
// If you have a remoting object that is
// initialised within the Config file
// initialise it here.
// RemotingConfiguration.Configure(fileName);
// Initialise
log.Info("OnStart: About to create XXX.");
XXX xxx = new XXX(); // Service implementation class
xxx.Initialise();
// We assume that if the XXX is not constructed and
// initialised correctly it will throw an exception.
// If we get to this point without an exception
// being thrown we assume 'alles ist gut'
startSuccesful = true;
log.Info("OnStart: Started \'" + ServiceDisplayName
+ "\' successfully=" + startSuccesful.ToString());
}
catch(Exception ex)
{
string errorMsg =
"Exception occured while starting service\'"
+ ServiceDisplayName
+ "\'. Expect an error message dialog");
log.Fatal(errorMsg, ex);
throw; // Results in error message: "XXX
// service on Local Computer started and
// then stopped. Some services stop if they
// have no work to do, for example, the
// Performance Logs and Alert service
}
if (startSuccesful)
{
// Tell the rest of the world we are running.
...
}
}
/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
log.Info("OnStop: Stopping \'" + ServiceDisplayName<br> + "\'");
if (xxx != null)
{
xxx.Dispose();
...
}
if (startSuccesful)
{
//
}
log.Info("OnStop: Stopped \'" + ServiceDisplayName<br> + "\'");
}
#endregion Service Methods
}
Subscribe to:
Posts (Atom)