Index: Scheduling/trunk/cs/bsdx0200GUISourceCode/CGDocumentManager.cs
===================================================================
--- Scheduling/trunk/cs/bsdx0200GUISourceCode/CGDocumentManager.cs	(revision 1050)
+++ Scheduling/trunk/cs/bsdx0200GUISourceCode/CGDocumentManager.cs	(revision 1051)
@@ -188,5 +188,7 @@
 
             
-            _current.InitializeApp();
+            bool isEverythingOkay = _current.InitializeApp();
+
+            if (!isEverythingOkay) return;
 
             //Create the first empty document
@@ -251,7 +253,7 @@
         /// See InitializeApp(bool) below
         /// </summary>
-		private void InitializeApp()
-		{
-			InitializeApp(false);
+		private bool InitializeApp()
+		{
+			return InitializeApp(false);
 		}
 
@@ -264,5 +266,5 @@
 		/// <param name="bReLogin">Is the User logging in again from a currently running instance?
         /// If so, display a dialog to collect access and verify codes.</param>
-        private void InitializeApp(bool bReLogin)
+        private bool InitializeApp(bool bReLogin)
 		{
             //Set M connection info
@@ -278,21 +280,24 @@
             m_ConnectInfo.EventPollingEnabled = false;
 
-            //Show a splash screen while initializing
+            //Show a splash screen while initializing; define delegates to remote thread
             DSplash m_ds = new DSplash();
             DSplash.dSetStatus setStatusDelegate = new DSplash.dSetStatus(m_ds.SetStatus);
             DSplash.dAny closeSplashDelegate = new DSplash.dAny(m_ds.RemoteClose);
-            DSplash.dAny hideSplashDelegate = new DSplash.dAny(m_ds.RemoteHide);
-
+            DSplash.dProgressBarSet setMaxProgressDelegate = new DSplash.dProgressBarSet(m_ds.RemoteProgressBarMaxSet);
+            DSplash.dProgressBarSet setProgressDelegate = new DSplash.dProgressBarSet(m_ds.RemoteProgressBarValueSet);
+
+            //Start new thread for the Splash screen.
             Thread threadSplash = new Thread(new ParameterizedThreadStart(StartSplash));
-            threadSplash.IsBackground = true; //expendable -- exit even if still running.
-            threadSplash.Start(m_ds);
-
+            threadSplash.IsBackground = true; //expendable thread -- exit even if still running.
+            threadSplash.Name = "Splash Thread";
+            threadSplash.Start(m_ds); // pass form as parameter.
+
+            //There are 20 steps to load the application. That's max for the progress bar.
+            setMaxProgressDelegate(20);
             
-          	//m_ds.SetStatus("Loading Configuration Settings...");
-            //m_ds.Refresh();
-			//this.Activate();
-            // smh--not used System.Configuration.ConfigurationManager.GetSection("appSettings");
+            // smh--not used: System.Configuration.ConfigurationManager.GetSection("appSettings");
+            
             setStatusDelegate("Connecting to VISTA");
-            //m_ds.Refresh();
+            
 			bool bRetry = true;
 
@@ -301,8 +306,5 @@
             //whether you can connect or not. Not good. So I test first whether
             //we can connect at all by doing a simple connection and disconnect.
-            //TODO: Make this more robust by sending a TCPConnect message and seeing if you get a response.
-
-            //m_ds.Refresh();
-
+            //TODO: Make this more robust by sending a TCPConnect message and seeing if you get a response
             if (m_Server != "" && m_Port != 0)
             {
@@ -313,11 +315,14 @@
                     tcpClient.Close();                  // then close it
                 }
-                catch (System.Net.Sockets.SocketException ex)
+                catch (System.Net.Sockets.SocketException)
                 {
-                    throw ex;
+                    MessageBox.Show("Cannot connect to VistA. Network Error");
+                    return false;
                 }
             }
 
-			do
+			
+            // Do block is Log-in logic
+            do
 			{
                 // login crap
@@ -347,9 +352,8 @@
                 catch (System.Net.Sockets.SocketException)
                 {
-                    MessageBox.Show("Cannot connect to VistA. ");
+                    MessageBox.Show("Cannot connect to VistA. Network Error");
                 }
-                catch (Exception ex)
+                catch (BMXNetException ex)
                 {
-                    //m_ds.Close();
                     if (MessageBox.Show("Unable to connect to VistA.  " + ex.Message, "Clinical Scheduling", MessageBoxButtons.RetryCancel) == DialogResult.Retry)
                     {
@@ -361,5 +365,5 @@
                         closeSplashDelegate();
                         bRetry = false;
-                        throw ex;
+                        return false; //tell main that it's a no go.
                     }
                 }
@@ -370,5 +374,6 @@
 
 			//Version info
-            //m_ds.Activate();
+            // Table #1
+            setProgressDelegate(1);
 			setStatusDelegate("Getting Version Info from Server...");
 
@@ -406,4 +411,6 @@
 
             //Change encoding
+            // Call #2
+            setProgressDelegate(2);
             setStatusDelegate("Setting encoding...");
 
@@ -414,5 +421,8 @@
                     m_ConnectInfo.bmxNetLib.Encoder = System.Text.UTF8Encoding.UTF8;
             }
-			//Set application context
+			
+            //Set application context
+            // Call #3
+            setProgressDelegate(3);
 			setStatusDelegate("Setting Application Context to BSDXRPC...");
 			m_ConnectInfo.AppContext = "BSDXRPC";
@@ -424,6 +434,8 @@
             string sCommandText;
 
+            //Schedule User Info
+            // Table #4
+            setProgressDelegate(4);
             setStatusDelegate(statusConst + " Schedule User");
-            //Schedule User Info
             DataTable dtUser = _dal.GetUserInfo(m_ConnectInfo.DUZ);
             dtUser.TableName = "SchedulingUser";
@@ -437,12 +449,16 @@
             m_bSchedManager = (sUser == "YES") ? true : false;
 
+            //Get Access Types
+            // Table #5
+            setProgressDelegate(5);
             setStatusDelegate(statusConst + " Access Types");
-            //Get Access Types
             DataTable dtAccessTypes = _dal.GetAccessTypes();
             dtAccessTypes.TableName = "AccessTypes";
             m_dsGlobal.Tables.Add(dtAccessTypes);
 
+            //Get Access Groups
+            // Table #6
+            setProgressDelegate(6);
             setStatusDelegate(statusConst + " Access Groups");
-            //AccessGroups
             LoadAccessGroupsTable();
 
@@ -454,6 +470,8 @@
             dtGroups.PrimaryKey = dcKeys;
 
+            //Get Access Group Types (??)
+            // Table #7
+            setProgressDelegate(7);
             setStatusDelegate(statusConst + " Access Group Types");
-            //AccessGroupType
             LoadAccessGroupTypesTable();
 
@@ -471,10 +489,14 @@
             m_dsGlobal.Relations.Add(dr);
 
+            //ResourceGroup Table (Resource Groups by User)
+            // Table #8
+            setProgressDelegate(8);
             setStatusDelegate(statusConst + " Resource Groups By User");
-            //ResourceGroup Table (Resource Groups by User)
             LoadResourceGroupTable();
 
+            //Resources by user
+            // Table #9
+            setProgressDelegate(9);
             setStatusDelegate(statusConst + " Resources By User");
-            //Resources by user
             LoadBSDXResourcesTable();
 
@@ -484,6 +506,8 @@
             m_dsGlobal.Tables["Resources"].PrimaryKey = dc;
 
+            //GroupResources table
+            // Table #10
+            setProgressDelegate(10);
             setStatusDelegate(statusConst + " Group Resources");
-            //GroupResources table
             LoadGroupResourcesTable();
 
@@ -500,6 +524,8 @@
             m_dsGlobal.Relations.Add(dr);
 
+            //HospitalLocation table
+            //Table #11
+            setProgressDelegate(11);
             setStatusDelegate(statusConst + " Clinics");
-            //HospitalLocation table
             //cmd.CommandText = "SELECT BMXIEN 'HOSPITAL_LOCATION_ID', NAME 'HOSPITAL_LOCATION', DEFAULT_PROVIDER, STOP_CODE_NUMBER, INACTIVATE_DATE, REACTIVATE_DATE FROM HOSPITAL_LOCATION";
             sCommandText = "BSDX HOSPITAL LOCATION";
@@ -519,6 +545,8 @@
             m_dsGlobal.Relations.Add(dr);
 
+            //Build ScheduleUser table
+            //Table #12
+            setProgressDelegate(12);
             setStatusDelegate(statusConst + " Schedule User");
-            //Build ScheduleUser table
             this.LoadScheduleUserTable();
 
@@ -529,6 +557,8 @@
             m_dsGlobal.Tables["ScheduleUser"].PrimaryKey = dc;
 
+            //Build ResourceUser table
+            //Table #13
+            setProgressDelegate(13);
             setStatusDelegate(statusConst + " Resource User");
-            //Build ResourceUser table
             this.LoadResourceUserTable();
 
@@ -545,12 +575,16 @@
             m_dsGlobal.Relations.Add(dr);
 
+            //Build active provider table
+            //Table #14
+            setProgressDelegate(14);
             setStatusDelegate(statusConst + " Providers");
-            //Build active provider table
             sCommandText = "SELECT BMXIEN, NAME FROM NEW_PERSON WHERE INACTIVE_DATE = '' AND BMXIEN > 1";
             ConnectInfo.RPMSDataTable(sCommandText, "Provider", m_dsGlobal);
             Debug.Write("LoadGlobalRecordsets -- Provider loaded\n");
 
+            //Build the CLINIC_STOP table
+            //Table #15
+            setProgressDelegate(15);
             setStatusDelegate(statusConst + " Clinic Stops");
-            //Build the CLINIC_STOP table
             // sCommandText = "SELECT BMXIEN, CODE, NAME FROM CLINIC_STOP"; //SMH
             sCommandText = "SELECT BMXIEN, AMIS_REPORTING_STOP_CODE, NAME FROM CLINIC_STOP";
@@ -558,6 +592,8 @@
             Debug.Write("LoadGlobalRecordsets -- ClinicStop loaded\n");
 
+            //Build the HOLIDAY table
+            //Table #16
+            setProgressDelegate(16);
             setStatusDelegate(statusConst + " Holiday");
-            //Build the HOLIDAY table
             sCommandText = "SELECT NAME, DATE FROM HOLIDAY WHERE DATE > '" + DateTime.Today.ToShortDateString() + "'";
             ConnectInfo.RPMSDataTable(sCommandText, "HOLIDAY", m_dsGlobal);
@@ -568,6 +604,6 @@
             //m_dsGlobal.WriteXmlSchema(@"..\..\csSchema20060526.xsd");
             //----------------------------------------------
-            
-
+
+            setStatusDelegate("Setting Receive Timeout");
             _current.m_ConnectInfo.ReceiveTimeout = 30000; //30-second timeout
 
@@ -575,8 +611,18 @@
             _current.m_ConnectInfo.ReceiveTimeout = 600000; //longer timeout for debugging
 #endif 
-			_current.m_ConnectInfo.SubscribeEvent("BSDX SCHEDULE");
-			_current.m_ConnectInfo.SubscribeEvent("BSDX CALL WORKSTATIONS");
-			_current.m_ConnectInfo.SubscribeEvent("BSDX ADMIN MESSAGE");
-			_current.m_ConnectInfo.SubscribeEvent("BSDX ADMIN SHUTDOWN");
+			// Event Subsriptions
+            setStatusDelegate("Subscribing to Server Events");
+            //Table #17
+            setProgressDelegate(17);
+            _current.m_ConnectInfo.SubscribeEvent("BSDX SCHEDULE");
+			//Table #18
+            setProgressDelegate(18);
+            _current.m_ConnectInfo.SubscribeEvent("BSDX CALL WORKSTATIONS");
+			//Table #19
+            setProgressDelegate(19);
+            _current.m_ConnectInfo.SubscribeEvent("BSDX ADMIN MESSAGE");
+			//Table #20
+            setProgressDelegate(20);
+            _current.m_ConnectInfo.SubscribeEvent("BSDX ADMIN SHUTDOWN");
 
 			_current.m_ConnectInfo.EventPollingInterval = 5000; //in milliseconds
@@ -586,4 +632,6 @@
             //Close Splash Screen
             closeSplashDelegate();
+
+            return true;
 			
 		}
