DevDisasters

C# Practice Makes Perfect

A simple fix cured the image sync issue, but in this case the fixer couldn't berate the pup who made the error.

For years, nobody cared that the legacy image-syncing application consumed as much bandwidth and processing time as it did.

Sure, it took 18 hours to sync 90,000 images, but considering the volume, how could anyone complain? Compared to a lot of other buggy enterprise-level applications in the organization, it was a tank.

After years of running under the radar, a server performance audit found that the "tank" didn't play nicely with system resources in its shared environment. The auditors' verdict: Unless someone was willing to switch what should've been a simple process to run on a costly dedicated server, the software performance needed to be fixed, ASAP.

Assigned to investigate and fix the problem, Jeff dove in.

Command-Line Console
Written as an intern's "learning .NET" project years earlier, the "tank" was a C#-based console application. It connected to a SQL Server database to retrieve a data set of content IDs and image URLs, and then fetched a bunch of JPG files.

Finding the logic -- along with the culprit code -- couldn't have been easier:

DataTableReader dtReader = img.CreateDataReader();
while (dtReader.Read())
{
    Console.WriteLine("Link " + dtReader["'id"] + ": " + dtReader["link"]);
    DataRow[] dataRow = dataimg.Where(x => x.columnXYZ == dtReader["id"]);
    try
    {
      string test = dataRow[0]["link"].ToString();
    }
    catch (IndexOutOfRangeException exaa)
    {
      if (String.Compare(dtReader["link"].ToString().Trim(), "") == 1)
      {
         comand = "C:\\tools\\curl\\curl.exe -k -H " + 
                   "\"Content-Type: image/jpg\" " + dtReader["link"] + 
                   " > " + Dpf1 + dtReader["id"] + ".jpg";
         StreamWriter myFile1 = new StreamWriter(fname1);
      myFile1.WriteLine(comand);
      myFile1.Close(); 

      Process getContentZIP = new Process();
      getContentZIP.StartInfo.FileName = @"x:\path\to\xyz.cmd";
      getContentZIP.StartInfo.WorkingDirectory = @"x:\path";
      getContentZIP.StartInfo.CreateNoWindow = false;
      getContentZIP.Start();
      getContentZIP.WaitForExit();
    }
  }
}

Temporarily ignoring the obvious quirks in the code, Jeff discovered that an error in a different section of the program ensured that the exception was always caught, resulting in its poor performance.

Spotty Execution
Hoping to stave off the auditors in the short term, Jeff made a quick fix so that the exception logic only executed when new or updated images needed to be fetched (instead of every time). This reduced the runtime to a fraction of what it originally was, much to the delight of the auditors and managers alike.

With the development out of the way, Jeff's most difficult task of all remained: He had to convince his coworkers that the intern who originally authored the C# console application -- who also happened to be named Jeff and coincidentally shared his last name -- wasn't him.

About the Author

Mark Bowytz is a contributor to the popular Web site The Daily WTF. He has more than a decade of IT experience and is currently a systems analyst for PPG Industries.

comments powered by Disqus

Featured

  • Get Started Using .NET Aspire with SQL Server & Azure SQL Database

    Microsoft experts are making the rounds educating developers about the company's new, opinionated, cloud-ready stack for building observable, production ready, distributed, cloud-native applications with .NET.

  • Microsoft Revamps Fledgling AutoGen Framework for Agentic AI

    Only at v0.4, Microsoft's AutoGen framework for agentic AI -- the hottest new trend in AI development -- has already undergone a complete revamp, going to an asynchronous, event-driven architecture.

  • IDE Irony: Coding Errors Cause 'Critical' Vulnerability in Visual Studio

    In a larger-than-normal Patch Tuesday, Microsoft warned of a "critical" vulnerability in Visual Studio that should be fixed immediately if automatic patching isn't enabled, ironically caused by coding errors.

  • Building Blazor Applications

    A trio of Blazor experts will conduct a full-day workshop for devs to learn everything about the tech a a March developer conference in Las Vegas keynoted by Microsoft execs and featuring many Microsoft devs.

  • Gradient Boosting Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the gradient boosting regression technique, where the goal is to predict a single numeric value. Compared to existing library implementations of gradient boosting regression, a from-scratch implementation allows much easier customization and integration with other .NET systems.

Subscribe on YouTube