网络断开时出现错误

时间:2014-09-05 00:37:00

标签: c# winforms

我有一个自动运行的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();

错误是:

  

从服务器接收结果时发生传输级别错误。(提供者:TCP提供者,错误:0 - 信号量超时期限已过期)

我尝试使用try-catch但是当网络断开连接时,我得到错误消息为1000次循环,直到网络连接并且程序恢复工作。我怎么能阻止它给我这个错误信息?

1 个答案:

答案 0 :(得分: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
        }
}