2014-09-05 41 views
0

我有一個自動運行的C#窗體窗體(因爲我在開始時設置了一個定時器)。它將讀取並在表格中插入一些記錄。這個程序給我錯誤,當網絡斷開連接。我希望它在網絡恢復後自動恢復工作。 這是程序:當網絡斷開連接時出現錯誤

private void UtilityForm_Load(object sender, EventArgs e) 
{ 
    timer = new System.Timers.Timer(); 
    timer.Elapsed += new ElapsedEventHandler(tmrProcess_Tick); 
    timer.AutoReset = true; 
    timer.SynchronizingObject = this; 
    timer.Interval = 1000; 
    timer.Enabled = true; 
} 

private void tmrProcess_Tick(object sender, EventArgs e) 
{ 
    ProcessLeads(); 
} 

private void ProcessLeads() 
{ 
    tmrProcess.Stop(); 
    lblActive.BackColor = Color.Red; 
    Application.DoEvents(); 

    //I m getting error here 
    TransmissionBuilder transmissionBuilder = new TransmissionBuilder(); 
    TransmissionAgent transmissionAgent = new TransmissionAgent(); 

    int leadCount = 0; 

    if (transmissionBuilder.Count > 0) 
    { 
     toolStripProgress.Maximum = transmissionBuilder.Count; 
     toolStripProgress.Minimum = 0; 
     toolStripProgress.Value = 0; 
    } 

    try 
    { 
     foreach (XDocument document in transmissionBuilder) 
     { 
      transmissionAgent.SendPingTransmission (transmissionBuilder.CurrentPingDocument); 

      if (transmissionAgent.PingWasAccepted) 
      { 
       transmissionAgent.SendLeadTransmission(
        transmissionBuilder.CreateLeadDocument(
         transmissionAgent.ReservationCode 
        ) 
       ); 
      } 

      TransmissionLog transmissionLog = new TransmissionLog(); 

      transmissionLog.WriteLogEntry(
       transmissionBuilder.CurrentApplicantId, 
       transmissionBuilder.CurrentPingDocument.ToString(), 
       transmissionAgent.PingResponse.ToString(), 
       transmissionBuilder.CurrentLeadDocument.ToString(), 
       transmissionAgent.LeadResponse.ToString(), 
       transmissionAgent.ReservationCode, 
       transmissionAgent.ConfirmationCode, 
       transmissionAgent.PingReason, 
       transmissionAgent.LeadReason, 
       transmissionAgent.PingWasAccepted, 
       transmissionAgent.LeadWasAccepted); 

      toolStripProgress.Value += 1; 
      lblMessage.Text = ++leadCount + " out of " + transmissionBuilder.Count.ToString() + " have been processed..."; 
      Application.DoEvents(); 

      if (!processIsRunning) 
      { 
       lblMessage.Text += " after " + leadCount.ToString() + " leads."; 
       toolStripProgress.Value = 0; 
       break; 
      } 
     } 

     transmissionBuilder.Dispose(); 
     toolStripStart.Enabled = true; 
     toolStripProgress.Value = 0; 

     if (leadCount == 1) 
      lblMessage.Text = "1 lead was processed."; 

     else if (leadCount > 1) 
      lblMessage.Text = leadCount.ToString() + " leads were processed."; 

      else 
      lblMessage.Text = "Process is waiting for leads to send..."; 
    } 
    catch (Exception e) 
    { 
     Utilities.LogError((long)transmissionBuilder.CurrentApplicantId, e.Message + e.StackTrace); 
     Utilities.WriteToFile(e.Message + "-----" + e.StackTrace); 
     lblMessage.Text = "Error while connecting with ACE Server. Retrying..."; 
    } 
    finally 
    { 
     lblActive.BackColor = Color.Green; 
     tmrProcess.Start(); 
     Application.DoEvents(); 
    } 
} 

它給了我的錯誤,在這些線路:

TransmissionBuilder transmissionBuilder = new TransmissionBuilder(); 
TransmissionAgent transmissionAgent = new TransmissionAgent(); 

的錯誤是:

A transport level error has been occured when receiving result from the server.(provider: TCP provider, error: 0 -The semaphore timeout period has expired)

我試圖把的try-catch但是當網絡斷開連接我得到的錯誤消息像1000次循環,直到網絡連接和程序恢復工作後。我如何防止它給我這個錯誤信息?

+0

請出示你的代碼,包括try-catch塊,你說你試過。 – 2014-09-05 00:45:45

回答

0

爲什麼不使用循環前Flag並設置爲true時出現錯誤,如:

Boolean hasShownMessage = false; 

foreach (XDocument document in transmissionBuilder) 
{ 
    // On catch change it 
    catch (Exception e) 
     { 
      if (!hasShownMessage) 
      { 
       Utilities.LogError((long)transmissionBuilder.CurrentApplicantId, e.Message + e.StackTrace); 
      Utilities.WriteToFile(e.Message + "-----" + e.StackTrace); 
      lblMessage.Text = "Error while connecting with ACE Server. Retrying..."; 
      } 
      hasShownMessage = True; // Set the flag to True 
     } 
} 
相關問題