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

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube