title: Problem in starting Sharepoint workflow
body: <p>I have a custom workflow that I am starting through code.The following code I am using to start a workflow.  </p>
  
  <pre><code>DeleteWorkflowTasks(properties.ListItem.ID);
                          Thread thread = new Thread(delegate() { StartApprovalWorkflow(); });
                          thread.Start();
  
  private void StartApprovalWorkflow()
          {
              try
              {
                  SPSecurity.RunWithElevatedPrivileges(delegate()
                      {
                          try
                          {
                              using (SPSite elevatedSite = new SPSite(siteID))
                              {
                                  using (SPWeb elevatedWeb = elevatedSite.OpenWeb(webID))
                                  {
  
                                      SPList calendarList = elevatedWeb.Lists[listID];
  SPWorkflowAssociation workflowAssociation = calendarList.WorkflowAssociations.GetAssociationByName(currentWorkflowName, System.Threading.Thread.CurrentThread.CurrentCulture);
                                      elevatedSite.WorkflowManager.StartWorkflow(itemBeforeUpdate, workflowAssociation, workflowAssociation.AssociationData);
                                  }
                              }
                          }
                          catch (SPException ex)
                          {
                                                      }
                          catch (Exception ex)
                          {
                                                      }
                      });
              }
              catch (SPException ex)
              {
  
              }
              catch (Exception ex)
              {
              }
          }
  </code></pre>
  
  <p>This code is attached with ItemUpdated eventhandler code.Whenever I update an item , it cancels the workflow which is already attached and creates a new workflow for the same item.Though it is working fine however sometimes the workflow is not getting started however the previous workflow is getting cancelled.
  The stack trace which I am getting is as follows.</p>
  
  <pre><code>Object reference not set to an instance of an object.   at Microsoft.SharePoint.Library.SPRequestInternalClass.OpenWeb(String bstrUrl, String&amp; pbstrServerRelativeUrl, String&amp; pbstrTitle, String&amp; pbstrDescription, Guid&amp; pguidID, String&amp; pbstrRequestAccessEmail, UInt32&amp; pwebVersion, Guid&amp; pguidScopeId, UInt32&amp; pnAuthorID, UInt32&amp; pnLanguage, UInt32&amp; pnLocale, UInt16&amp; pnTimeZone, Boolean&amp; bTime24, Int16&amp; pnCollation, UInt32&amp; pnCollationLCID, Int16&amp; pnCalendarType, Int16&amp; pnAdjustHijriDays, Int16&amp; pnAltCalendarType, Boolean&amp; pbShowWeeks, Int16&amp; pnFirstWeekOfYear, UInt32&amp; pnFirstDayOfWeek, Int16&amp; pnWorkDays, Int16&amp; pnWorkDayStartHour, Int16&amp; pnWorkDayEndHour, Int16&amp; pnMeetingCount, Int32&amp; plFlags, Boolean&amp; bConnectedToPortal, String&amp; pbstrPortalUrl, String&amp; pbstrPortalName, Int32&amp; plWebTemplateId, Int16&amp; pnProvisionConfig, String&amp; pbstrDefaultTheme, String&amp; pbstrDefaultThemeCSSUrl, String&amp; pbstrAlternateCSSUrl, String&amp; pbstrCustomizedCssFileList, String&amp; pbstrCustomJSUrl, String&amp; pbstrAlternateHeaderUrl, String&amp; pbstrMasterUrl, String&amp; pbstrCustomMasterUrl, String&amp; pbstrSiteLogoUrl, String&amp; pbstrSiteLogoDescription, Object&amp; pvarUser, Boolean&amp; pvarIsAuditor, UInt64&amp; ppermMask, Boolean&amp; bUserIsSiteAdmin, Boolean&amp; bHasUniquePerm, Guid&amp; pguidUserInfoListID, Guid&amp; pguidUniqueNavParent, Int32&amp; plSiteFlags, DateTime&amp; pdtLastContentChange, DateTime&amp; pdtLastSecurityChange, String&amp; pbstrWelcomePage)
     at Microsoft.SharePoint.Library.SPRequest.OpenWeb(String bstrUrl, String&amp; pbstrServerRelativeUrl, String&amp; pbstrTitle, String&amp; pbstrDescription, Guid&amp; pguidID, String&amp; pbstrRequestAccessEmail, UInt32&amp; pwebVersion, Guid&amp; pguidScopeId, UInt32&amp; pnAuthorID, UInt32&amp; pnLanguage, UInt32&amp; pnLocale, UInt16&amp; pnTimeZone, Boolean&amp; bTime24, Int16&amp; pnCollation, UInt32&amp; pnCollationLCID, Int16&amp; pnCalendarType, Int16&amp; pnAdjustHijriDays, Int16&amp; pnAltCalendarType, Boolean&amp; pbShowWeeks, Int16&amp; pnFirstWeekOfYear, UInt32&amp; pnFirstDayOfWeek, Int16&amp; pnWorkDays, Int16&amp; pnWorkDayStartHour, Int16&amp; pnWorkDayEndHour, Int16&amp; pnMeetingCount, Int32&amp; plFlags, Boolean&amp; bConnectedToPortal, String&amp; pbstrPortalUrl, String&amp; pbstrPortalName, Int32&amp; plWebTemplateId, Int16&amp; pnProvisionConfig, String&amp; pbstrDefaultTheme, String&amp; pbstrDefaultThemeCSSUrl, String&amp; pbstrAlternateCSSUrl, String&amp; pbstrCustomizedCssFileList, String&amp; pbstrCustomJSUrl, String&amp; pbstrAlternateHeaderUrl, String&amp; pbstrMasterUrl, String&amp; pbstrCustomMasterUrl, String&amp; pbstrSiteLogoUrl, String&amp; pbstrSiteLogoDescription, Object&amp; pvarUser, Boolean&amp; pvarIsAuditor, UInt64&amp; ppermMask, Boolean&amp; bUserIsSiteAdmin, Boolean&amp; bHasUniquePerm, Guid&amp; pguidUserInfoListID, Guid&amp; pguidUniqueNavParent, Int32&amp; plSiteFlags, DateTime&amp; pdtLastContentChange, DateTime&amp; pdtLastSecurityChange, String&amp; pbstrWelcomePage)
     at Microsoft.SharePoint.SPWeb.InitWeb()
     at Microsoft.SharePoint.SPWeb.get_UserInfoListId()
     at Microsoft.SharePoint.SPListItem.CalculateEffectivePermMask(SPBasePermissions permIn)
     at Microsoft.SharePoint.SPListItem.get_EffectiveBasePermissions()
     at Microsoft.SharePoint.SPListItem.DoesUserHavePermissions(SPBasePermissions permissionMask)
     at Microsoft.SharePoint.Workflow.SPWorkflowManager.StartWorkflow(SPListItem item, SPWorkflowAssociation association, String eventData, Boolean isAutoStart)
     at Microsoft.SharePoint.Workflow.SPWorkflowManager.StartWorkflow(SPListItem item, SPWorkflowAssociation association, String eventData)
  </code></pre>
  
  <p>Appreciate any help on this.</p>