Monday, 1 February 2016

Start and Stop Windows Services on Visual Studio Build.


Introduction

If you have a windows service it is handle to get Visual Studio build to stop the service before build then restart it after it completes the build.


Setup your service

run this from an admin dos command prompt

sc.exe create myService binpath= "C:\services\MyService.exe" DisplayName= MyService



Pre-Build Event
Its in the project properties in Visual Studio, Build Events tab.

This will stop the service.

set svcname=MyService
net stop %svcname%
EXIT 0



Post-Build
Its in the project properties in Visual Studio, Build Events tab.

This copy the files built and start the service. Will also create the c:\services directory if it doesn't exist.

IF NOT EXIST "C:\services" MD "C:\services\";
attrib -r "C:\services\*.*" /s
:while1
Copy "$(TargetDir)*.*" "C:\services";
IF ERRORLEVEL 1 goto :while1



To Debug The service

Attach to the process number (PID) in the Services Tab in Windows Task Manager I've found to be the easiest way.





Wednesday, 16 December 2015

C# Handle Exceptions on TASK

Tasks are great, run stuff in the background and let the UI without any bad freezing or usual problems with calling webservices or intensive tasks.

But Handling exceptions in called Task is easy enough when you know how

The Key is looking at the Task.IsFaulted bool.


  • Task.IsFaulted  is true :    The task was ended on an exception.
  • Task.IsFaulted  is false:    The task was ended normally (no exception)


Exceptions Returned by Task

The exceptions are returned as a collection, the top exception is fairly useless and you have look at the innerException to find the real cause for the issue.


What if I don't handle Exceptions

Well!! you code will run as the Task was run successfully and your calling layer/ui may be in a state that is less than ideal and your application may stop working and it may (read will)  take a long time for you to find out or debug the exact issue.  Tasks without exception handling are black holes waiting to happen.



CODE


 // Run Stuff in BackGround
 Task.Factory.StartNew(() =>;
 {
     //Do this in the background
      SomeIntensiveJob();
                
  }).ContinueWith(task =>;
   {
     //Check to see if there was a exception on the task handle it.
     if (task.IsFaulted)
      {
        string exMessage = "unknown error";
        string exStack = "unknow Exception is null";

        if (task.Exception != null)
         {
                       
          //Log all the Exceptions (maybe lots)
          foreach (var ex in task.Exception.InnerExceptions)
          {
             exMessage = ex.Message;
             exStack = ex.StackTrace;
             string errlog = $"Stuff , Exception : {exMessage} ,  Stack :  {exStack} ";
             og(errlog);
           }

         }

       //Update Your UI here with Errors
       Label.Text = $"Error on stuff {exMessage}, with stack {exStack}";
       return; // exit on error 
  } //////////////////////////  end exception handling ///////////////////////////////////

  // do this on the UI thread once the task has finished..
  // The task has completed without any issues so do your stuff
  runPostTaksStuff();

 }, System.Threading.CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());

Thursday, 19 November 2015

C# WPF Window to be Dialogue to Win32


The Windows Form Method

This is how its done on Windows Forms forms, fairly straight forward.

//apphandle  is the application handle for your Win32 application
IntPtr windowHandle = new IntPtr(Convert.ToInt32(apphandle));
WindowHandleWrapper windowWrapper = new WindowHandleWrapper(windowHandle);
MyWinForm = new MyForm();

MyWinForm.ShowDialog(windowWrapper);


But WPF forms don't support 

public DialogResult ShowDialog(IWin32Window owner);



Doing this for WPF Forms

You do this by setting the owner of the WPF window/form.

// apphandle  is the application handle for your Win32 application
IntPtr FormHandle = new IntPtr(Convert.ToInt32(apphandle));

// Create new WPF Form
var app = new MyWpfWindow();

// New Helper 
var helper = new WindowInteropHelper(app);

// Point the FormHandle to owner of the 
helper.Owner = FormHandle;
app.ShowDialog();




Easy eh ?  

Wednesday, 24 June 2015

Getting Non Escaped Double Quotes in Strings


I discovered this today by sheer chance and trying it will trying to build some dynamic Oracle scripts I was building.

Use single quotes in a StringBuilder to enclose a double quote  SingleDoubleSingle :  '"'

   StringBuilder sb = new StringBuilder();
            var col = "NameCol";
            sb.Append('"' + col + '"');
            sb.Append(" = @NAME ");


So easy when you know how!

Friday, 15 May 2015

Persist Server side Object Between PostBacks on ASP.NET

This is handy if you want a collection to persist between postback, good for general prototyping or testing.


Set the Item to static as shown below.



        private static MyData  DAL { get; set; }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;

            DAL = new MyData();
            grid1.DataSource = DAL.Customers;
            grid1.DataBind();
        }





Monday, 20 April 2015

Telerik RadSpell Stop Spell Dialogue from Moving

Problem

This control (and other in Telerik) control set have a quirk that on dialogue drop it enables all other iframes. So if you have a hidden iframe or dialogue then this will pop up as visible.

What it should really do is only enable items that are not hidden before it starts the drag.

The drag is not limited so it can be dragged off your application



Stop the Drag

To stop the drag do this.


  $('.rwTitlebar').prop("disabled", true);




Monday, 23 February 2015

Raspberry PI Snappy Unbuntu Username and Password


OK What is it???


I'm sure its not a big secret but it seems so when you look for it.

Username : unbuntu
password:   unbuntu


It is not  pi / raspberry that for Raspbian.

I'm really only waiting for the Windows 10 core to be released....