Tuesday, August 30, 2011

How to show current elapsed time on a WinForm using background workers and stopwatch

Problem:
I wanted to show the time elapsed for time-consuming SQL command. This is so the user could see that something is actually happening.

Solution:
I accomplished this by using two BackgroundWorkers, a status strip, two status strip labels, and a stopwatch. This will update the time label every half second and stop when the data thread is done.


 //Make sure to add these two
using System.Diagnostics;
using System.Threading;
        private void btnSQLCommand_Click(object sender, EventArgs e)
        {
                lblDataStatus.Text = lblStatus.ToolTipText = lblDataStatus.ToolTipText = "";
                lblStatus.Text = "00:00:00";
                button1.Enabled = false; //So user cant click button while operation is running
                bgWorkerTime.RunWorkerAsync();
                bgWorkerData.RunWorkerAsync();

            }           
          
        }

        private void bgWorkerTime_DoWork(object sender, DoWorkEventArgs e)
        {
            Stopwatch sw = Stopwatch.StartNew();
            while (!bgWorkerTime.CancellationPending)
            {
                TimeSpan ts = sw.Elapsed;
                bgWorkerTime.ReportProgress(0, String.Format("{0:00}:{1:00}:{2:00}",
                    ts.Hours, ts.Minutes, ts.Seconds));
                Thread.Sleep(500);
            }
            sw.Stop();
        }

        private void bgWorkerTime_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            lblStatus.Text = e.UserState.ToString();
        }

        private void bgWorkerData_DoWork(object sender, DoWorkEventArgs e)
        {
            string status = "";
                        /*
                        ...Execute SQL Command...
                              Update lblDataStatus.Text to show what's going on
                        */
                if (status != "Success")
                {
                    lblDataStatus.ToolTipText = status;
                    lblDataStatus.Text = "Error occurred";
                    break;
                }
            }
            if (status == "Success")
                lblDataStatus.Text = "Operation completed successfully";
           
        }

        private void frmDemo_FormClosing(object sender, FormClosingEventArgs e)
        {
          //Only attempt to close the form if both bg workers are not working 
            if (bgWorkerTime.IsBusy || bgWorkerData.IsBusy)
                e.Cancel = true;
            if(bgWorkerTime.IsBusy)
                bgWorkerTime.CancelAsync();
            if(bgWorkerData.IsBusy)
                bgWorkerData.CancelAsync();
        }

        private void bgWorkerData_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
                     //When the background worker executing the SQL command is completed,
                     //it will shutdown the timer bg worker, and enable the button
            bgWorkerTime.CancelAsync();
            btnSQLCommand.Enabled = true;

        }
References
1. Mainly this http://stackoverflow.com/questions/2067919/objectdisposedexception-running-stopwatch-in-gui-thread

Thursday, August 4, 2011

Connecting to TFS 2010 with VS 2008

1. install VS 2008 Team Explorer
2. install VS 2008 Service Pack 1
3. install the Visual Studio Team System 2008 Service Pack 1 Forward Compatibility Update for Team Foundation Server 2010 (VS90SP1-KB974558-x86.exe)
4. Edit registry
  1. Start->Run->Regedit
  2. Navigate to HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\TeamFoundation\Servers
  3. Right-click and add a string with Name="Whatever the hell you want",                              Value=http://<ip or hostname of tfs server>:<port>/tfs/<collection name>
 5. Restart VS

now when you go to add a project to source control you should see "Whatever the hell you want" in the server list, and the url will be the value of the regkey you put in.

References
1. http://www.jmedved.com/2009/11/visual-studio-2008-and-team-foundation-server-2010/
2. http://stackoverflow.com/questions/1925512/visual-studio-2008-cant-connect-to-known-good-tfs-2010

Wednesday, August 3, 2011

Citrix XenApp installation / config guide reference

This is a really good guide for installing Citrix XenApp 5:
http://carlwebster.com/learning-the-basics-of-xenapp-5-for-windows-server-2008-part-1-of-7/

There's 7 different parts with a step by step processing including screenshots. This is great for a Citrix noob like me!
There was an error in this gadget