Domanda

C # 2005

Sto usando un lavoratore sfondo per elaborare alcune informazioni di accesso. Tuttavia, il lavoratore sfondo deve fermarsi e attendere 2 eventi accadano. Al termine, il lavoratore sfondo può completare il suo lavoro. Sono callback che chiamerà il metodo set () della AutoResetEvent.

Quindi, io sto usando AutoResetEvent per impostare quando questi 2 eventi hanno finito. Tuttavia, mi sembrava di essere sempre questo messaggio di errore: "Eccezione generata dalla destinazione di una chiamata".

E Eccezione interna L'indice era fuori portata. Deve essere non negativo e inferiore alla dimensione della collezione. Nome del parametro:. Indice"

L'eccezione di solito spara quando il successo di registrazione lascia portata.

Molte grazie per qualsiasi consiglio

Il codice per il lavoratore di sfondo.

// Waiting for 'Account in use' and 'Register success or failure'
AutoResetEvent[] loginWaitEvents = new AutoResetEvent[]
{
        new AutoResetEvent(false), 
        new AutoResetEvent(false)
};

private void bgwProcessLogin_DoWork(object sender, DoWorkEventArgs e)
{
      Console.WriteLine("Wait until event is set or timeout");
      loginWaitEvents[0].WaitOne(3000, true);

      if (this.accountInUseFlag)
      {
                if (this.lblRegistering.InvokeRequired)
                {
                    ///this.lblRegistering.Invoke(new UpdateRegisterLabelDelegate(this.UpdateRegisterLabel), "Account in use");
                }
                else
                {
                    ///this.lblRegistering.Text = "Account in use";
                }
                // Failed attemp
                e.Cancel = true;
                // Reset flag
                //this.accountInUseFlag = false;
                return;
       }
       else
       {
                // Report current progress
                //this.bgwProcessLogin.ReportProgress(7, "Account accepted");
       }

        Console.WriteLine("Just Wait the result of successfull login or not");
        loginWaitEvents[1].WaitOne();
        Console.WriteLine("Results for login registionSuccess: [ " + registerSuccess + " ]");

        if (this.registerSuccess)
        {
                // Report current progress
                //this.bgwProcessLogin.ReportProgress(7, "Register Succesfull");  
                // Reset flag
                //this.registerSuccess = false;
        }
        else
        {
                if (this.lblRegistering.InvokeRequired)
                {
                    //this.lblRegistering.Invoke(new UpdateRegisterLabelDelegate(this.UpdateRegisterLabel), "Failed to register");
                }
                else
                {
                   // this.lblRegistering.Text = "Failed to register";
                }
                // Failed attemp
                e.Cancel = true;               
                return;   
        }
}

// Wait for the callback to set the AutoResetEvent

// Error sometimes happens when the function leaves scope.
private void VaxSIPUserAgentOCX_OnSuccessToRegister(object sender, EventArgs e)
{
        Console.WriteLine("OnSuccessToRegister() [ Registered successfully ]");
        this.registerSuccess = true;
        this.loginWaitEvents[1].Set();
} 


// If the flag is not set, then just time out after 3 seconds for the first LoginWaitEvent.waitOne(3000, true)
 private void VaxSIPUserAgentOCX_OnIncomingDiagnostic(object sender, AxVAXSIPUSERAGENTOCXLib._DVaxSIPUserAgentOCXEvents_OnIncomingDiagnosticEvent e)
{
        string messageSip = e.msgSIP;

        //Indicates that a user is already logged on (Accout in use).
        string sipErrorCode = "600 User Found"; 
        if (messageSip.Contains(sipErrorCode))
        {
            // Set flag for account in use
            this.accountInUseFlag = true;
            Console.WriteLine("OnIncomingDiagnostic() WaitEvent.Set() accountInUseFlag: " + this.accountInUseFlag);
            loginWaitEvents[0].Set();   
        }
}
È stato utile?

Soluzione

Non è più probabile un errore di indicizzazione nel metodo UpdateRegisterLabel.

Ottenere una traccia stack dell'eccezione interna, dovrebbe puntare più da vicino a dove è.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top