20 June 2007

Check if url is alive, some code

private static bool IsURLAlive(string strURL)
{

using (WebClient myWebClient = new WebClient())
{
char[] buf = new char[128];
try
{
using ( Stream myStream = myWebClient.OpenRead(strURL) )
{
StreamReader sr = new StreamReader(myStream);
sr.ReadBlock(buf , 0 , 127);
myStream.Close();
}
}
catch ( WebException webe )
{
return false;
}
return true;
}

}

11 June 2007




Inter process communication with events and delegates

Architecture Design


The Remote Object Process Requires A Reference To Each Class That Subscribe To The Event. In order for a remote server object to wire up an event (or register a callback) with a client, the server object needs to know a little bit about the client. This requires the server object to have a reference to each client that will subscribe to its events. This seems like a huge limitation for remote events. Imagine creating a server object to be used over remoting from many different types of client objects, and being required to add a reference to each of these client objects in the server object. Additionally, we may want to instantiate remote objects from within a client executable, and have the executable handle the remote event. Unfortunately, we cant add an executable as a reference to our server object.
So, having a client executable subscribe to remote objects events might seem impossible. However, there is a good way to get around this limitation with the use of a small event helper class, or event shim.
The idea is to create a small class that has a basic purpose of subscribing to remote objects events and then raising its own event. At first, this might seem like we are only postponing our event problem by propagating it up one level, but were really not. Remember, the real problem is our server object needs a reference to the client that will be subscribing to its event.
By placing a reference to this small event helper class in both the client and server assemblies, we circumvent the problem.
The server can see the object that will be subscribing to its event (the event helper) so it will know how to wire up the delegate callback for the event, and because our client has a reference to this small helper object it is able to subscribe to events on it.
IPC Event Design






Command Pattern Example
Recently i asked to add a functionality to an existed code platform, some of the functionality execute before the operational platform and some after.
This leads to massive change all over the code, in order to refine the change i use the command pattern

The Solution

Create an interface which define the command call it IWrapCommand, Define the functions as command, part of the functions refer as before function and the other as after function.
The OperationWrapper use to wrap the code platform with the commands.
The before command execute in the constructor and the after command execute in the dispose.
It preferred to use the using(){} code pattern to wrap the code platform.
Here the code,
namespace OperationWrapping{ public interface IWrapCommand { void Execute(); }}

using System;
using System.Collections.Generic;
using System.Text;

namespace OperationWrapping
{
/// <summary>
/// The class add the abillity of wrapping a operational process with
/// commands before the operation process executin and after.
/// </summary>
public class

OperationWrapper : IDisposable
{
#region Data members
private List< IWrapCommand > m_commandsAfter = null;
#endregion

public OperationWrapper(IWrapCommand i_commandBefore ,
IWrapCommand i_commandAfter)
{
if (i_commandAfter != null)
{
m_commandsAfter = new List< IWrapCommand >();
m_commandsAfter.Add(i_commandAfter);
}

if (i_commandBefore != null)
{
i_commandBefore.Execute();
}
}

public OperationWrapper(List<IWrapCommand> i_commandsBefore ,
List<IWrapCommand> i_commandsAfter)
{
m_commandsAfter = i_commandsAfter;

if (i_commandsBefore != null)
{
foreach (IWrapCommand m_command in i_commandsBefore)
{
m_command.Execute();
}
}
}

///<summary>
///Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///</summary>
///<filterpriority>2</filterpriority>
public void Dispose()
{
if (m_commandsAfter != null)
{
foreach (IWrapCommand m_command in m_commandsAfter)
{
m_command.Execute();
}
}

GC.SuppressFinalize(this);
}
}
}


namespace OperationWrapping
{
public interface IWrapCommand
{
void Execute();
}
}

AlfaCommand alfaCommand = new AlfaCommand();
DirtyCommand dirtyComamnd = new DirtyCommand();

using (OperationWrapper m_OperationDirty = new OperationWrapperalfaCommand , dirtyComamnd))
{
if (alfaCommand.IsContinue)
{
// the old code wrapped
}
}

29 May 2007

Scroll text into view in Rtf known as Rich Text Box, How to do that ?

1. The simple way, Just set the HideSelection property to false.

2. The geek way, SendMessage(this.Handle, (uint)WM_HSCROLL, (System.UIntPtr)3, (System.IntPtr)0);

The geek way can be implemented in every scrollable control.

08 November 2006

Notes on clickonce,

As Microsoft said clickonce is only attend to support light application - recommended.
Why Microsoft doesn't write it on clickonce abstraction page i know, but, in case you have such big application and you would like to give the user the feeling of web application , so maintenance will more easy, updating and versioning will be support on ease way (almost automatically), SE demand for web application but you have only winform application so click once is the result.

Where is the catch,
Registry, in case your application write to registry or you use file extension then you need to support it on other way, think using files as URL and file extension will be dropped.
Why ? Because registry demand for admin privilege, most of the cases the user is user and not admin, speak on maintenance !.

Shortcut, Forget on customize the shortcut.
You can customize but uninstall the application will not remove the zombie customized shortcut's.

Command line, Supports only with online strategy, forget from being offline and using the query string, why Microsoft does it to us ? why ? there is no technology limit why it does no support but there is no way how to do that so offline will be run with query string, note the only the app ref file can run the application in offline mode.

What to do,
  • Do not use registry at all (clickonce mode), in case you need the application tun also part of clickonce and as common application (winform + msi), so support registration by XML files.
  • Use shared directory Locate it under user application data (do not use the roaming user application data folder), use the application setting base architecture and provide provider which will read and write from the customize shared place.
  • In case your application need to support run files (accordance the application spec), then a url refer to shared resources (not part the clickonce deployment) with url to the application can replace the file extension use (in some cases).

Bye.

01 November 2006

Think on mspaint but in 3d, just amazing, this project very similar the MIT project called assit but more powerful.

You can download it and run it, You need jave runtime, simple installtion.

Enjoy.
http://www-ui.is.s.u-tokyo.ac.jp/~takeo/teddy/teddy.htm

Imperial

Coke and Mentos - The Show

31 October 2006

Important article on application setting .net 2.0

The old way and the new way.
COM Self Registration - With interop dll
Referring to interop file with self regitstration ability just refer to the com dll and set the property Isolated to True and press save, then run the tlbimp with the dll and generate the interop file for that com and refer to it, the interop will be self registry for that com object part of the application.

30 October 2006

Click once Part 1

Suppose we have a desktop application(winform) and we need to convert it (get it) worked in web environment, Hard to maintain

Microsoft develop the new platform of having by click application execution, run from the network or install from the network (add shortcut) in local computer or event the legacy way of CDROM.

Called ClickOnce, Microsoft ship those platform with nice tools which ease to develop and handle so migrating your application to click once can be very easy and fun.

Things you need to look for when deploy ClickOnce in (for) your application,

• Security - Its hard to maintain secutiy permission sets acquired for the application deployment because each change in development phase can react on the deployment phase which is too risky.
I prefer to set the fulltrust for the application because my application almost do and will do everythings as ; IO, event log, Env, Registry and etc, So why to become involved in that story.

• Com object - Self registration is the key very simple in .net 2.0 (2005) just change a property and everythings will work, Why to use that / As all you know com object demand registration on local machine registry in order to work (Its a component), Read more on : Deploying COM Components with ClickOnce.

More issues i will update during my research and work on clickonce

29 October 2006

Something I encounter, in case you have got system which have thread on the air run in the background and they have their own mechanism of monitoring, in case the application trying to exit Application.Exit, or application.ExitThread, What you think will be, If you think your application is closing, so you are wrong, open the task manager and its still on the air, in previous 1.1 .Net framework it worked properly and application was really exit but now its not, So what you are going to do is To call Environment.Exit(code) and it will close.
Hello,

Thought you write code and run it and oppos the computer is shut down, ha !
So its happened to me and I tried to figure out why, what cause to that bug - a critical one, so Here the code who can shut down the computer you on it, Remember, I have no warranty to the code and its conclusions.

So,

Open a modal dialog and write this code, i.e. when the dialog closing event occurred

Win32.SetForegroundWindow(this.Owner.Handle);

this.ShowInTaskbar = false;


(must be in the same order).

Enjoy or not.