Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts

Wednesday, 12 March 2014

State of affairs in programming for Mobiles - an opinion!

I think that we are slowly getting used to the fact that most people do most of their work now on non-desktop devices. In fact, on a recent trip away, my laptop stopped charging and I was left to look after server management and a few database bugs on nothing but tablet and phone. Thanks to 4G for that one!

However, as mobile devices (including tablets) become more ubiquitous, this means that developers are having to learn new skill-sets or broaden existing talent. The following information is just a quick summary of information and opinions that I am forming with regard to mobile development.

Choices
When you take on a new project for mobile devices, the first thing that you are likely to have to decide from a programmatic point of view is whether to have a single code base or to write more natively for each device.

To explain: iOS uses the increasingly popular Objective-C as a native language. Whereas Android uses Java. I'm not sure about Blackberry as I have never had any one ask for an app, however, it seems as though c++ would probably be the language of choice for that. The benefit is that you can write straight for the intended operating system and will have full access to the SDKs and APIs that make development easier on those platforms. At present, for the scale of apps that I develop, this is the weapon of choice for me.

Alternatively, you can write non-native code that accesses the APIs indirectly. Perhaps the best example that caught my eye was Xamarin, primarily because it allows you to write code in c#. The obvious benefit is the re usability of code. The write once, use anywhere syndrome that made Java popular. Other companies do have similar libraries (another notable c# entry is DevExpress).

So the question is, why do I still use native code rather than cool 3rd party frameworks that allow you to write a common set of libraries?

Firstly, cost is a big thing. A lot of these libraries ask for large lump sum payments and/or subscriptions. When developing, I am rather scrooge like and will try to get anything that I can for free before I go to the paid option.

Secondly, I am quite thick and I found that with the small amount of time that I have spent on learning native languages vs learning c# for Xamarin or DevEtreme, I actually progressed quicker learning a new language. I find that the Data Access and common tasks are far more simple to learn from the docs of the native languages and I really love the MVC way that Objective-C utilizes! (but that's for another post me-thinks).

Finally, I have bad experiences with write once libraries. Without going too deeply into the reasons (again another post's job), I have found that there are a lot of issues and bugs that you simply don't get in native libraries that transfer to non-native libraries. This is certainly true of desktop equivalents and I have found similar sentiments regarding the mobile libraries that has put me off for now. What I really don't want is to be promised a write once scenario only to have to continually put "if" statements or other conditional branching for each operating system. I actually would prefer to write it all again (but with the logic already in mind) than to do that.

Again, this post is highly subjective and some of you may find different scenarios work for you than what I have outlined above. If you have any comments, please feel free to comment below and tell me where I am wrong.

Saturday, 3 August 2013

Writing a silent updater in c#

So recently I have been working on some software for windows for a company here in the UK. The software does lots of cool stuff including taking orders and products from Quickbooks and syncing with a website.

One of the things that I wanted to do, was to avoid having to release an update individually to each user that has the software installed (this could be quite a few down the line). Therefore, I thought that I would write a simple auto-updater that works behind the scenes. In the making of it there were a number of new tricks that I learnt that I thought that I would share with the world. As always, if you see a security/efficiency issue, please let me know either in the comments or you can contact us direct:
daniel@casserlyprogramming.com

How it works
The basic design was that a windows service would run and check regularly, whether or not the software was up to date. If it is not, then it downloads the latest version and overwrites the needed files. As this is a fairly small project without massive files (and is unlikely to be such), this approach is acheived in the following way:

  • The service has a timer. The timer runs every 5 mins.
  • If the software is open, we don't do the update (point 1)
  • There is a version stored on a database that is accessible from the service. The local version is stored in an ini file. If the versions don't match we perform the update. 
  • We download a zip file which is the files that have changed since the last update (with the version number as the name of the zip) (point 2)
  • Then we unzip and overwrite the files in the installation directory of the program (the service exe is shipped in the same directory so that we can unzip the files straight to the root of the program. (point 3)
  • Then we update the local version number and log the update (I also send myself an email to state that <this computer name> has updated to <the version number>). 
All that the software itself does is on load it checks if the service is installed. If not it installs it (which in turns starts the service). If it is installed it checks if it is running. If not it starts the service. Simples!

Point 1 - Checking that the software is running
I found some code that seems to work fairly well and finds if any Process is running:

//--------------------------------------------------------------------------------  public bool IsProcessOpen(string name)
{
     foreach (Process clsProcess in Process.GetProcesses())  
    {
       if (clsProcess.ProcessName == name)   
      {
         return true;   
      }
    }
    return false;
}

Then I pass in the process name for my software to check that if it is running.

Point 2 - Downloading Files
This was something fairly new to me. However may come in handy. In order to do this (I presume there are multiple ways as usual) simply, the following code will help:
using (WebClient client = new WebClient())
{
   client.DownloadFile("http://urltozip.com/" + new_version.Replace(".", "-") + "-EXE.zip", new_fn);
}

Point 3 - Unzipping files
I found an excellent library for using standard zip files in c# (unlike python the standard zipping/compressing is not really that useful IMHO).
http://dotnetzip.codeplex.com/

using that all I needed to do was the following to get my files out of the zip:

 using (ZipFile zip = ZipFile.Read(new_fn))     
{
           foreach (ZipEntry ent in zip)         
              ent.Extract(".", ExtractExistingFileAction.OverwriteSilently);    
}

A few points that are important here:

  • The using is very important. We need to dispose of the ZipFile object (according to the readme). 
  • The "." is the directory we want to unzip to. In my requirements this was the same directory as the zip. An empty string will cause an error. 
  • The second parameter is an enumeration. I chose the silent overwrite for obvious (I hope) reasons. 
Conclusion
This only took me a few hours of coding and Googling. Therefore, we will see how it stands the test of time. I imagine that there are more elegant ways of doing a lot of this and this will work for my requirements much better than probably most other software.

Again, if you have any comments, please feel free to let me know.

Sunday, 19 August 2012

Emacs vs Vim vs Notepad

I like a little controversy on  a Sunday. Therefore, I thought I would spend my Sunday afternoon, curtains drawn to stop the glare, typing a little article to describe my humble opinions on the biggest question that faces the modern man.

EMACS VS VIM VS NOTEPAD!!

I have seen MANY articles from biased, dogmatic sources that claim one or the other is the pinnacle of text editing, and whilst I don't agree that it is not an important subject, I am going to try to be neither biased nor dogmatic in my analysis here.

Also a note before we get in to the nitty gritty of the article; I am BY NO MEANS a power user of ANY text editor. I have made some minor adjustments to my main text editor (which will remain nameless according to the above paragraph) but as I do most of my work in HTML, CSS , Javascript , Python and C#; most of the time I am in one of the following:
  • Visual Studio
  • Eclipse
  • Aptana Studio
I have also been known to dabble with Butterflies now and again.

I am going to tackle the editors in reverse order to the title as these are the orders that I tried them out first.

NOTEPAD
Notepad, Notepad2 and Notepad++ are what I would call traditional editors. Notepad++ has an AWESOME tabbing feature distinctly lacking in the other two, notepad 2 has debatabely better syntax highlighting and notepad is probably faster.

I like Notepad (for this section this encompasses all three above mentioned programs) as it is simple to use and requires NO brainpower which is a massive boon for someone like me.

Further more, as far as graphic user interfaces go on a text editor, I would say that any of the Notepads are far and above nicer than the other two that we will discuss today. However, they all are really just for inputting text and have no further use other than that. Notepad++ does have more in the way of plugins and probably could be put in to its own category for that reason alone, but none of the customization that is possible in Emacs is available as easily with Notepad. 

VIM
Vim was the first "keyboard only" text editor I tried. This is my own phrase for Vim and Emacs as I don't really know what to call them otherwise.

Vim is an interesting idea. You have an input mode and I forget what they call it but basically and editing mode. Switching between the two is as easy as clicking on i or esc (depending on current mode) and moving around the document can all be done using the keyboard (without going over to the arrows)

The principle idea was great for Vim, and I know people who swear by it, however, I found it immensely illogical and the learning curve just seemed to intense. I found that I was still looking up the docs after a day of using it to do simple things and I also found that looking up the docs was not fun.


EMACS

I tried Emacs when I got bored of Vim. I was put on to the idea by a blog (I forget which, sorry!) and thought that it looked like a great alternative to Vim.

The important thing for me was to not have to move over to the arrow keys and also to be able to bulk perform actions. This was easily possible in Emacs and in fact, working through the tutorial allowed me to learn and remember these commands quickly.

Which brings me to my next and possibly most difficult to explain point. I found Emacs more logical, more easy to remember and perhaps most importantly for me, more fun in documentation than Vim. As I was in the market for a quick text editor that I would invest some time in, this won me over.

However, the down side to using either Vim or Emacs is that you will need almost exclusively need to use them in order to stop yourself from getting in to bad (notepadesque) habits or from having a lot of missing characters when using normal notepad. The thing is that it is SO different using these "keyboard only" text editors, that you will find yourself as frustrated with it as if you were going from touch typing QWERTY to touch typing DVORAK. However, in a similar way there are benefits to spending the time in learning to use them wisely.

One final thing to note about the emacs editor is that it has a language (emacs lisp) that allows you turn Emacs in to much more than a text editor. However, due to time restraints and laziness I never looked deeply enough into it for it to ever be more than just a novelty.


CONCLUSION
I am still undecided as to which the best of the three are. Due to a massive amount of laziness and the fact that the IDEs that I use have only buggy support at best for EMACS I tend to use NOTEPAD more often. Is this because it is better? Honestly, I don't know, I suppose it is for me is the best answer I can give.

Vim was just not fun enough for me and I have no interest in learning any more about it unless someone can give me a non-subjective reason to. Emacs is great, however, I always felt that I was using a tool at about 1% of its potential and because of this I never really got the benefit of all the hard work that I was putting in mentally to using it. Maybe one day I will get on to using it more fully if ever I have to leave my IDEs behind. At the moment the only language I use EMACS for is C and that is VERY rare. 

Tuesday, 3 July 2012

Equals or ==?

So Android is pretty ubiquitous now. New tablets/phones coming out with the version 4 upgrades and awesomeness. I personally love my Android phone. No, it's not expensive, but I love it!

However, this is where I get to moan. I have never really touched Java before Android came out. Some people swear by it, I swear at it! It's not that Java is an aweful language, in fact there are some really nice features. However, from a C# point of view it is somewhat like learning Polish after learning Russian.

You see the slavic languages are very similar. The have a similar verb conjugation and many similar verbs, and then you get to the important bit, the verb that is the difference between like and love and it lets you down and changes. (Russian love transliterated is "lubit" in Polish lubic is to like).

The same thing happened with Java. I'm feeling confidant in the fact that I can write code without looking up syntax just because it's SO similar to C#. So much so that when i evaluated two variables with a

     if(var1 == var2) { // Do Something... }

statement, saw in debugging that they had the same value I expected that the // Do Something... code would run. WRONG! Turns out that the == in Java is COMPLETELY different to C#!

"EEEJIT!" you say. "Java is a better and older language than C#"

"No need to be rude!" Say I "C/C++ is an even older an more widely known langauge than even Java"

So how do we get around this evilness? Why does this happen?

Java-Samples states : "the equals( ) method compares the characters inside a String object. The == operator compares two object references to see whether they refer to the same instance"

So the equals() method was what I needed. Tbh I had not been so dissapointed by a language inconsistancy since the lack of switch in python. I look forward to comments and suggestions (clean ones please) about what I'm missing in Java that makes it better than either C# and/or Python. 

Tuesday, 8 May 2012

Ubuntu 12.04 First Thoughts and synergy fix.

As I am rather an impulsive sort of person I thought that I would give an opinion on Ubuntu 12.04 only a few days after upgrading my server machine. (Server as in I keep my important files that I share with other machines, not as in I server apache websites!)

So my first thought? Well I was really excited by the idea of the HUD - not just because it seems like a term from a gaming interface, but also because the idea means we can escape EVER bringing a space hogging ribbon bar over to ubuntu (result!!). However, to note that Libre office (a default Ubuntu app) does not support this beast makes me want to cry in my cornflakes. ;-( However, the dream is there for future releases.

So the next thing I looked at was the black background that it gave me? Why? I had used one of the default wallpapers in the previous release of Ubuntu. A lovely autumnal walkway reminiscent of a parisienne path. However, on the new update, my favorite wallpaper has sadly been extinguished! With no real replacement amongst the defaults (and the fear of losing it again next release) I decided to use a picture. At least that is easy in Ubuntu!

Now today, and the reason for sharing this article, I'd had enough of clicking ctrl, stopping synergy, starting synergy and then going through the whole process next time i need the ctrl, shift or alt keys. Apparently the latest version of Ubuntu is incompatible with the stable release of synergy.

Google to the rescue and it appears that the Beta is fine. So I installed direct from the website on the Linux machine. At this point it must be noted that I am a moron, especially as when I tried to run it I nearly threw the cat across the room (a plan foiled due only to a lack of feline in the flat) when it still did not work. Then the moment of eureka when I remembered that the Windows machine would most probably need updating too.

Now as updates to beta versions of products go, I am yet to find anything that I do not like and particularly on the Windows version of Synergy. User interface is easier than going deaf in a night club and I was back up and running in no time. What's more, the ctrl button works!!!

So, what do I think of Ubuntu 12.04? For me it has caused numerous, small headaches. However, I LOVE Ubuntu. I was talking to collegue in the office the other day who said that he could not wait to get home to work on Ubuntu again (the office is nearly all Windows 7/XP). He could not stop thinking about it! Whilst he might just be the saddest person I know, I share his sentiments about using Linux in general and the rush that it gives you. Furthermore, the user base and community (aside from bearded zealots) are superb and what's more, IT'S FREE!

So the big question is, will I ever do all of my work on Ubuntu? hmmmn, probably not anytime soon, C# is still mainly a Visual Studio beast, despite the advancements of Mono. Although, given time and incentive (this mainly means when Windows 8 is released and I can't take any more metro, ribbon bar in the explorer, which is by the way a complete break of Microsoft's own usage terms for the Ribbon bar and HUGE BUTTONS that are useless not on a mobile device after being hit with a hammer on the fingers.......) I might just move over to the lovely Linux for good. Here's to the future!

Tuesday, 1 May 2012

Android Programming - Yes I know it's Java!!

So this week has lead to me getting to that point in my interminable list of projects to do where I finally bit the bullet and started doing some development for Android.



The first thing that struck me with Android development is Java. I could not believe that I was being forced in to such an unusually bloated language for such a small device. However, I instinctively feel that the guys behind it must have a much better undestanding than me so, let them get on with it. The interesting thing is that it is not a standard JVM but rather runs on the Dalvik VM.

So I decided to go for the new kid on the block Intellij Idea as an IDE. I looked into the Eclipse, and didn't much like it. I installed and almost immediately went with Intellij and haven't looked back. I realize this is subjective, however, I don't care! ;-)

The second thing I learnt was that you need to have a kettle handy for the first time you run the emulator. Closely related is the fact that you never want to close the emulator until you are sure that you are done debugging. This is somewhat due to the fact that the time it takes for the emulator to load the first time is comparable to my grandmother crossing the road, walking up the stairs and putting a brew on for me!

But when you get past all of that, I was really impressed with the layout tools, the xml resources and how easy they were to use, the simple differences between activities, services and broadcast receivers and how they all fit together with the Java language. (it nearly turned me to Java for desktop apps but not quite!) Everything by default is well layed out in the various folders and I feel that I automatically know where I should look for changes to be made after only a day looking into it, and I am no master by any stretch.

A few more interesting features took my eye (and it is possible that these are IDE specific):

[1] Awesome debugging and logging in the Android tab
[2] Git support  (oh yes!!!!)
[3] The DDBM (Dalvik Debug Monitor) has Emulator control where you can call the phone. Great for the app I am writing and also for feeling that you have friends without having to talk to them on the phone!!
[4] Good support network. I can't belive that in the relatively short time that Android has been about, I can find someone else somewhere who has done, is trying to do, will do something that I am trying to do and documented it online (go to love Google for that). This is something that even Python and C# have not yet managed despite the slightly longer life they have led.

So all in all, I really like programming for the Android and would recommend it to anyone! Programming is fun again!! Actually it was always fun but now we have an emulator which means that debugging is more interesting for my long-suffering wife :-)

Sunday, 1 April 2012

Welcome to my blog

Hi all. This blog is going to be all about programming and other aspects of life that are touched by computer use. I am not a programming master and as such I often come across new things that I learn in my travels. As I do so I will share them here and you all can get involved too.

One thing that I feel passionate about in the sphere of programming is the place of Open source and community software. Therefore, if I produce a tool, library or other set of code that I think will be useful, I will definately share it on either sourceforge or github (or both if I am feeling energetic.)

Contact me for more information or for a full CV!