Mar 2 2010

New MSDN Subscription Level:Essentials

Category: Desktop and ServerJoel Ivory Johnson @ 10:38

Microsoft is making some adjustments to the Visual Studio and MSDN licensing with the release of Visual Studio 2010. For customers that purchase Visual Studio via retail the MSDN Essentials Subscription will be available. MSDN Essentials Subscribers will have access wo Windows 7 Ultimate Edition, Windows Server 2008 R2, SQL Server Datacenter R2, and 20 hours of Windows Azure usage. Presales of Visual Studio will open on 9 March 2010 at the Microsoft store and other select stores.

Found via Somasegar's Weblog

Tags:

Feb 27 2010

Sharing Source Files Among Projects

Category: Desktop and Server | MobileJoel Ivory Johnson @ 06:01

There are times when you will want to share the same source code among several projects. A common way to do so is with a shared assembly; you put common functionality in one project and then share the output among several other projects. But at times this solution isn't suitable such as when you have functionality that you plan to share across more than one .Net runtime (ex: Desktop Framework, Compact Framework, and Silverlight Runtime). For these cases you can copy your source code to the projects for all three run times. But then you end up with three branches of code and may need to make sure thay are synced up with each other.

It is possible to use the same source file in different projects by adding a link for the file from another project so that each project is using the same runtime. Since it is the same physical files changes to the file done from one project are visible to all the projects using the same linked file. Adding a linked file is easy. To link to one file's project from another right-click on the project, select Add->Existing Item and navigate to the file. Once you've found the file click on it and then click on the down triangle on the Add button and select "Add as Link."

A potential problem from using this solution is you may have items in a class that you don't want to be visible in another class. You can selectivly hide sections of code using a few preprocessor directives. As a simple example let's say I made a Windows Form application and I have all of the files from it linked to a second project. I have code in a method that is setting the text on a label. But I want the text to be set differently depending on the project in which it is run. The preprocessor directives I will use are #if, #else, and #endif.

#if App1
            txtMyMessage.Text="Hello from App1";
#else
            txtMyMessage.Text = "Hello from App2";
#endif

In the above code only the C# code in the #else, block will be compiled. The code in the #if block will be ignore. For my first project I want to code in the #if block to be used. To accomplish this I need to add a Conditional Compilation Symbol. I right-click on the project and select Properties. Under the Build tab I can add conditional compilation symbols. I've done this for the first project and have added a symbol named App1. So now the first block of code will get compiled and the second block ignored.

While this solution has it's advantages it is not the end-all solution for sharing functionality across projects. If you find yourself using excessive conditional compilation blocks in your code then you may have reached a point at which it is better off having two seperate source files.

Tags:

Feb 25 2010

Avoid the Undocumented and Unsupported functions

Category: Desktop and Server | MobileJoel Ivory Johnson @ 11:53

Between the MSDN forums and some e-mails that I've received I am seeing a common theme among some of the questions; they deal with undocumented features of Windows phones. My advice when it comes to such features is that it is better to avoid them than to try an figure out how to make something work with an undocumented feature.

One might call me lazy for this, but it is not from laziness that I give this advice. Using undocumented features comes with some risks. One risk is that a program that uses an undocumented feature may have reduced compatibility across devices and firmware versions. My first encounter with this is when I was writing an article on Windows Mobile Power Management. During the time that I was writing it an update was made available for my TyTn II from Windows Mobile 6.0 to 6.1. After the update some of the code examples I had written no longer worked because of changes in the power manager.

If you look through the MSDN documentation on Windows Phones you'll see that it is mixed with the documentation on Windows Embedded CE (which makes since given that that Windows Phone operating systems are derived from Windows Embedded CE. On some of the pages you'll see that a certain feature is supported on Windows Embedded CE but not Supported on Windows Mobile. Sometimes when you will see a feature listed as supported on Windows Embedded CE but not on Windows Phones even though you can find Windows Phones that have the feature implemented. For items like this you'll find that the functionality may be optional (and thus not on all Windows phones) and/or the implementation may have a lot of dependencies on OEM decisions (and may not work the way that you would expect). Another example is customizing a notification icon that shows up in the task bar. Doing this was agains the Made for Windows Mobile guidelines. And if you take a look at Windows Mobile 6.5.3 you'll see that this old technique may not work on newer devices

The ramification of making programs that require functionality that may or may not be present and may or may not behave a certain way when present is that your program could end up with limited compatibility and inconsistent stability across devices.

The same functions are not undocumented and unsupported to every one. OEMs (who are implementing the device, drivers, and portions of the operating system) will have documentation and support for many of the low level functions available in Windows. Manu of these functions aren't mentioned in MSDN documentation. Application developers typically need higher level access to the device and generally less of these functions will be available to them.

I'm not labelling all instances of using undocumented and unsupported features as bad. There are actually quite a few scenarios in which this provides some pretty nice solutions. When applied with discipline everything has its place. So I'm stating this more as a general rule and thing that for the general case these areas of functionality are better left untouched for general development.

Tags:

Feb 24 2010

Visual Studio 2010 Resources RC Resources

Category: Desktop and Server | MobileJoel Ivory Johnson @ 10:59

@MSDN_Forum posted some good resources on getting started with Visual Studio 2010 Release Candidate.

Tags:

Feb 8 2010

This collection already contains an address with scheme http. There can be at most one address per scheme in this collection.Parameter name: item

Category: Desktop and ServerJoel Ivory Johnson @ 16:21

I was trying to deply a WCF service to one of my web servers the other day and ran into a problem. I kept getting the following error message:p>

This collection already contains an address with scheme http. There can be at most one address per scheme in this collection.Parameter name: item

The problem didn't happen on my local machine but did on the web server making it a little difficult to figure out what was causing it. It happened on the server because my web server is in a shared hosting environment in which case the WCF service also needs to know the host header. To do this I navigated to <system.serviceModel> in the web.config and added the following:

<serviceHostingEnvironment>
<baseAddressPrefixFilters>    
    <add prefix=http://MyHostHeader />
</baseAddressPrefixFilters>
</serviceHostingEnvironment>

Tags:

Dec 27 2009

Handling Cookies with Redirects and HttpWebRequest

Category: Desktop and ServerJoel Ivory Johnson @ 15:08

The HttpWebRequest handles redirects automatically. That's usually a nice feature but it can actually get in the way when the web server is setting cookies in the same response in which it is sending a redirect. For some odd reason the HttpWebRequest object will totally discard cookies that are set with a redirect response. I ran into a problem with this when I was working in some code that interfaced to Google Voice and it was driving me crazy since I didn't know where the problem was coming from.

Once I figured out what was happening the solution to the problem was simple. The first step is to disable the class's automatic response to redirect request. When a response is returned from the class it's necessary to check the response to see if it includes a redirect and if so create a new request. Since a redirect could occur more than once it is necessary to do this in a loop. With each new WebRequest that is created it is necessary to set the CookiesContainer member.

A simplified version of my code looks like the following:

HttpWebRequest GetNewRequest(string targetUrl)
{
     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUrl);
     request.CookieContainer = SessionCookieContainer;
     request.AllowAutoRedirect = false;
     return request;
}


HttpWebRequest request = GetNewRequest(targetUrl);
HttpWebResponse response= (HttpWebResponse)request.GetResponse();

while (response.StatusCode == HttpStatusCode.Found)
{
     response.Close();
     request = GetNewRequest(response.Headers["Location"]);
     response= (HttpWebResponse)request.GetResponse();
}

//--At this point the redirected response is ready to be used

Trying to perform this same thing on the .Net Compact Framework is a little more challenging since it doesn't support cookies at all. I'll discuss the solution I used in another post within the next few days.

Tags:

Nov 30 2009

Improved FindControl for Windows desktop and mobile

Category: Desktop and Server | MobileJoel Ivory Johnson @ 14:45

A few days ago I posted some code containing an implementation for a Windows Forms version of FindControl. Aviad P. pointed out a correction and a way that the routine can be simlified. While I had intended the original code to do abreadth-first search it was doing a combination of depth and breadth. The functionality could also be implemented with a single loop. The result of the messages the resulting code from our communication is below.

Control FindControl(string target) {
    return FindControl(this,target);
}
static Control FindControl(Control root, string target){
    if(root.Name == target)
        return root;
    List currentLevel = new List<Control>() { root };
    while (currentLevel.Count > 0)
    {
        Control match = currentLevel.FirstOrDefault(x => x.Name == target);
        if (match != null) return match;
        currentLevel = currentLevel.SelectMany(x => x.Controls.Cast<Control>()).ToList();
    }    return null;
}

Tags: ,

Nov 17 2009

Up to 800 Answers in the Microsoft Forums

For those that don't know I've been active in the Microsoft Forums for a little over a year.  I've been keeping track of how many answers I've provided. It took me about a year to reach 500 answers in August. It is now November and I am up to 800 questions.  Im active in more Windows Phone related forums now. That's why my rate of answering questions has gone up.

800 questions

View my profile here.

Tags:

Nov 9 2009

Sleeping for Zero Seconds

Category: Desktop and Server | MobileJoel Ivory Johnson @ 02:44

I was asked about something that appeared to be an oddity in some example code that I posted online. The code in question was a single call to the sleep function.

Thread.Sleep(0);

or if you prefer to see the native code version:

Sleep(0);

The sleep function is often used to insert delays into code. But what does it mean to inert a delay of zero? This will make more sense if we first take a look at the API documentation. I pulled this description from the native API documentation for the sleep function:

This function causes a thread to relinquish the remainder of its time slice and become unrunnable for an interval based on the value of dwMilliseconds.

So there are two actions performed by calling this function.

  1. Thread relinquishes it's time slice
  2. Thread becomes unrunnable for the interval specified

Sleeping for zero seconds will cause the thread to relinquish control but it will remain runnable. What does it mean for a thread to be runnable? To understand this one must understand how multitasking works. At its simplest level a single core processor can only run one thread or program at a time. The illusion of multiple programs doing something at the same time is maintained by rapidly switching control from one program to another. When the operating system is deciding which thread to switch to next one of the factors is which threads are runnable. A thread can become unrunnable voluntarily (by calling the sleep function or using other thread related APIs) or the thread may make a call that requires some time for an event to complete, such as an IO request to a drive or network device. When the thread is made runnable again then it is reintroduced as a canidate thread for receiving time to execute.

When a program calls Sleep(0) it is never made unrunnable. It just allows for the operating system to go ahead and schedule the next thread for execution.

When is it Appropriate to Use Sleep(0)?

Most often I use Sleep(0) in code in which the main program loop is not being driven by messages and events. For programs that are driven by messages and events if the program is not receiving any messages (such as notification of user actions, GPS coordinates changing, timers expiring, so on) then the program is not doing anything. Programs of this nature spend a majority of their life not doing anything. Though the user typically won't perceive these programs that way. Programs that are not dependent on messages and events to run (typically games or media intensive applications) execute continuously and can quickly consume CPU bandwith. Throwing the Sleep(0) into the main loop of such a program will give other programs a chance to execute (as opposed to appearing locked up due to another process monopolizing the CPU) and in some cases the Sleep(0) can help better preserver battery life.

Tags:

Jun 8 2009

Reporting Services Templates

Category: Desktop and ServerJoel Ivory Johnson @ 08:57

This past week I worked on a Reporting Services project.  I was asked to change the style sheet that reporting services used.  But the style sheet used by reporting services doesn't apply in the way that many expect.  As stated in this microsoft support document the stylesheet only affects the parameter section and not the report.  I came up with a solution that I've used before (And I'll put the details of the solution in another post).  Part of my solution involved making reporting templates which other developers would use for deriving their reports.  If you ever make a report template that you want to be available when you add a new report just add the RDL's that form your template to the following path:

C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PrivateAssemblies\ProjectItems\ReportProject

Tags:

Jun 3 2009

Microsoft Sponsored Azure and Live Services Training

Category: Desktop and ServerJoel Ivory Johnson @ 13:47

If you live in the Atlanta area and would like to take advantage of a full day of Microsoft sponsored Azure training RDA and Microsoft have an event scheduled for July 2nd that you may want to take advantage of.

Please join us at the Microsoft Atlanta area (Alpharetta) campus on July 7th, 2009, as we present a free day of Microsoft-sponsored training on the Microsoft Azure platform and Live Services. No prior experience with Azure or Live Services is required.
For more information and to sign-up , please visit http://www.rdacorp.com/getting_started/events.html.

Tags:

Apr 1 2009

After a year of XNA

Category: Desktop and ServerJoel Ivory Johnson @ 22:14

I just got an e-mail notifying me that renewal time for my XNA subscription is in another month.  I signed up for XNA development a year ago.  It cost 99 USD/year.  After paying the 99 USD one is able to debug and run .Net based XNA games on the Xbox 360.  I signed up not because I was interested in making games but because the Xbox 360 is a nice piece of hardware.  It's got 6 cores, 2 of which are dedicated to the operating system leaving 4 cores for the developer to use as he or she sees fit.  I had planed to try out some graphic and AI algorithms on the 360 and experiment with some multiprocessor programming techniques (not to be confused with multithreading).

So what have I done in the past year?  Nothing. A shortcoming of the development tools for the XNA framework is that they don't work on multihomed computers (computers with more than one active network card).  Conventional thinking may lead one to try to disable the other network cards that are not being used.  Unfortunatly that wasn't an option for me.  The VM software that I have installed creates several virtual network adapters, none of which can be disabled.  So for the past year my 99 USD has given me very little.

This year will be different.  I purchased a new computer that I only plan to use for recreational computing (which will include programming the xbox 360). I won't be installing any VM software on the new machine.  Hopefully I'll be able to get started with something this weekend.

Tags:

Jan 22 2009

The Global Economy and the Terminal Redux

Category: Desktop and ServerJoel Ivory Johnson @ 09:45

Earlier today Microsoft announced its plans for adjusting to the world economy. The news story I read stated that mid-PC sales were down while server software was up.  I can't help but associate that with what a Sony exec called "A race to the bottom" in which consumer and OEMs begin to target building the cheaper computer instead of the higher performing computer.   With the increasing emphasis being placed on Cloud Computing and Web Services and it all supports a prediction that software was going to move from the individual PCs back to the servers.  We can find evidence of this now.  Previously if you wanted to work on a word document you needed a computer with sufficient space and a reasonably powered processor.  You would then need to purchase and install Microsoft Word and you could edit documents from your siloed computer.  While you can still do this today you also have the option of using an online service to essentially do the same thing.  You can use a low powered computer with any of a variety of operating systems as long as that computer has a supported browser.   The power of the individual computer matters a little less and connectivity matters a whole lot more. as applications begin to target modern terminals. 

It's possibly that my view of the future is slightly exagerated but I doubt it is completely wrong.  That being said my plans for adjusting to what I think to be the needs of the future are to accelerate my learning path for Microsoft Azure Services Platoform and Live Services. I've been experimenting with Live Services for some time now and have found them to be useful in quickly putting together useful applications.  An Azure application runs across several 64-bit Windows 2008 servers.  Installation of patches is handled for you, failover is taken care of, and so are several other maintenance tasks allowing development of highly available, secure, and redundant applications with more ease.   Effective use of the platform is going to require a different way of thinking than developing the traditional application so I plan to get started tonight with the intent of having information to teach and share within the next week.

Tags: