Jun
24th | 2008

How Do You Exit Your .Net Application?

Posted by Shahar Y |
Filed under .Net, C# |

image

The question in the title does not refer to the actions you take when you are about to exit your application, issues like logging, closing file handles, freeing unmanaged resources and so on. It does literally refer to the exit action itself:

  • Do you use Application.Exit or Environment.Exit?
  • Where do you call the exit method?

The idea for that post came to my mind after I had to solve a weird bug where pressing the Exit button led to a freeze in an application that I work on. It turned out that I was not using the proper Exit method although I was aware to the fact that there are 2 possibilities. Sometimes, when you are in a middle of a coding momentum, writing a lot of code, you can miss the little details. Those will come back to hunt you later on,  and the most difficult thing about it is that everything was functioning fine for about a year before the bug showed his ugly face.

Lets first understand the difference between the 2 Exit methods:


Continue Reading...
Jun
2nd | 2008

A Programming Job Interview Challenge #6 - C# Games

Posted by Shahar A |
Filed under C# |

The sixth of the series of programming job interview challenge is out. Other then commenting the solution, I remind you that you can post the solution on your blog and get a link next week!

Here is the solution to the previous challenge:
Two-way merge sort (External Sorting) : The idea is breaking the big file into subfiles, sorting them and than merging them back together. In the first pass read one page at a time, the records in the page are sorted (quicksort for example) and the page is written back to disk. in subsequent passes, each pair of sorted output from the previous pass are read and merged to produce sorted subfile twice as long:

1. Read each page, Sort it, Write it back
2. while more than one sorted subfile:
while subfiles from previous pass left to merge:
- Choose next two subfiles from previous pass
- Read each subfile into a memory page (one page at a time)
- Merge the subfiles (remember each of them is already sorted), write output to a memory page
- Write output page to disk when needed.


Continue Reading...
May
30th | 2008

How to Find Memory Leaks With CLRProfiler

Posted by Shahar A |
Filed under C# |

We all know managed code can have memory leaks. You can find a good example here: A .NET memory leak you did not think about. Microsoft provides us with the CLR Profiler, an open source tool for analyzing the behavior of your managed application, which you can download here. It contains very good documentation about the different functions of the tool, however I still find it a bit hard to start with, so here is a simple step-by-step example of how to use it. After you finish downloadoing it , extract the files and open the directory. there you will find the manual, you can read it later… Navigate to CLRProfiler\Binaries\x86 (or x64) and run CLRProfiler.exe.


Continue Reading...
May
27th | 2008

How to Create a WPF Two Level Master-Detail Application With ComboBoxes

Posted by Shahar A |
Filed under C#, WPF |

Master-Detail is a pattern for displaying details of a specific item selected from a list of items. In this post I’ll demonstrate how to use master-detail in WPF with two levels of objects, each displayed in a ComboBox and Data Binding to tie them together.


Continue Reading...
May
22nd | 2008

Why ServiceContainer Doesn’t Work With Remote Objects

Posted by Shahar A |
Filed under .Net, C#, Misc. |

The ServiceContainer, implementing the IServiceContainer Interface is used to store and retrieve services. it is used for Dependency Injection, a very important concept that allows separating independent components, and is widely discussed over the web. However, one drawback of this implementation is it does not work with .net  remoting…


Continue Reading...
May
16th | 2008

Why Pluggable Applications Fail in NUnit?

Posted by Shahar Y |
Filed under .Net, C#, Misc. |

I am currently working on a project with a pluggable application (load plugins at runtime and execute them) that loads assemblies at runtime, using the Assembly.LoadFile() method. I wanted to test it using NUnit (feel free to read our 3 Ways to Run NUnit from Visual Studio post). Although, the code was functioning very well it always failed during the NUnit tests, always! It took me several days to understand what went wrong and I want to share you with my findings.


Continue Reading...
May
15th | 2008

How to Convert List(T1) to List(T2)

Posted by Shahar A |
Filed under .Net, C# |

Did you ever need to convert List(T1) to List(T2)? One example might be when implementing an interface. you might need to expose a collection of other interfaces (or maybe the same interface), But you usually hold the concrete type implementing the interface in the collection. Lets look at the following example:


Continue Reading...
May
13th | 2008

Beware of List.Find()

Posted by Shahar A |
Filed under .Net, C# |

List<T>.Find() returns the first element found that matches a given criteria. So lets say we have List<int> and we use the Find method on it. What will be the returned value? If a number in the list matches the criteria it will be returned, but what if not? what does the variable zero contains after executing this code:

List<int> listOfInts = new List<int>(new int[] { 1,2,3,4,5,6,7,8,9});
int zero = listOfInts.Find(
    delegate(int i)
    {
        return i == 0;
    });

It has the value of…


Continue Reading...
May
7th | 2008

Why Should You Wrap Your ASP.NET Session Object

Posted by Shahar A |
Filed under .Net, ASP.Net, C# |

ASP.NET provides mechanisms for storing information for a single user session or across multiple sessions. This is done using the HttpSessionState and HttpApplicationState classes. The Page class has Application and Session attributes to provide access to current objects. The simple way to access them is as following:

if (Session["FirstName"] == null)
{
    LabelFirstName.Text = “FirstName”;
}
else
{
    LabelFirstName.Text = (string)Session["FirstName"];
}
if (Session["LastName"] == null)
{
    LabelLastName.Text = “LastName”;
}
else
{
    LabelLastName.Text = (string)Session["LastName"];
}


Continue Reading...
May
5th | 2008

A Programming Job Interview Challenge #2

Posted by Shahar Y |
Filed under .Net, C# |

Job Interview ChallengeLast week I posted A Programming Job Interview Challenge which was very successful, both in the amount of page views and, in the amount of comments and mails we received. This fact made us (the Dev102 team) decide to add a weekly programming job interview challenge column to www.Dev102.com.


Continue Reading...

Search Dev102