Archive for Maggio 26th, 2008

Start External Application

CodeKeep C# Feed Maggio 26th, 2008

Description: A simple method of starting an external application

Link: http://www.codekeep.net/snippets/f915fc5b-a717-4f2e-a21e-fdf3cfe95947.aspx

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClass, string lpTitle); 

/// <summary>
/// Starts a specified app if not running
/// </summary>
/// <param name="appName">The name of the app</param>
/// <param name="pathToYourExe">The path to the app exe</param>
void StartSpp(string appName, string pathToYourExe)
{
	if (FindWindow(null, appName) == System.IntPtr.Zero)
        {
            Process.Start(pathToYourExe);
        }
}

Serialize Code

CodeKeep C# Feed Maggio 26th, 2008

Description: Stuff

Link: http://www.codekeep.net/snippets/b66bcc9e-3fa7-4d49-b173-0cfded008ce0.aspx

		/// <summary>
		/// Cache the gpEncounter to DB
		/// </summary>
		private void SaveToCache()
		{
			// Serialize to a memory stream
			MemoryStream stream = new MemoryStream();
			DataContractSerializer serializer = new DataContractSerializer(typeof(GpEncounter));
			serializer.WriteObject(stream, _gpEncounter);
			// Just make sure we're pointing at the beginning of the stream
			stream.Seek(0, SeekOrigin.Begin);
			// Save stream to the DB
			CachingDataAccess.CacheGpEncounter(_guid, stream);
			//Clean up
		}
		/// <summary>
		/// Retrieve the GpEncounter from the DB
		/// </summary>
		private void RetrieveFromCache()
		{
			byte[] rawdata = CachingDataAccess.UnCacheGpEncounter(_guid);
			MemoryStream stream = new MemoryStream(rawdata);
			DataContractSerializer serializer = new DataContractSerializer(typeof(GpEncounter));
			stream.Seek(0, SeekOrigin.Begin);
			_gpEncounter = (GpEncounter) serializer.ReadObject(stream);
		}

		public static bool CacheGpEncounter(Guid guid, MemoryStream gpEncounterMemoryStream)
		{
			Database db = DatabaseFactory.CreateDatabase("StatConnection");
			byte[] bytes = gpEncounterMemoryStream.ToArray();//NB Do Not use GetBuffer()
			string sqlCommand = @"Delete from Cache where CacheGUID = @guid;
								INSERT INTO Cache (CacheGUID, Blob) VALUES  (@guid, @data )";
			DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
			db.AddInParameter(dbCommand, "guid", DbType.Guid, guid);
			db.AddInParameter(dbCommand, "data", DbType.Binary, bytes);
			db.ExecuteNonQuery(dbCommand);
			return true;

		}
		public static Byte[] UnCacheGpEncounter(Guid guid)
		{
			Database db = DatabaseFactory.CreateDatabase("StatConnection");
			string sqlCommand = @"SELECT Blob FROM Cache WHERE CacheGUID = @guid";
			DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);
			db.AddInParameter(dbCommand, "guid", DbType.Guid, guid);
			DataTable table = new DataTable();
			byte[] bytes = (byte[]) db.ExecuteScalar(dbCommand);

			return bytes;
		}