المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Multi-threading using AutoResetEvent



C# Programming
05-17-2009, 12:00 AM
Hello,

C# 2005

I am using a background worker to process some login information. However, the background worker has to stop and wait for 2 events to happen. Once these have finished the background worker can complete its job. They are callbacks that will call the Set() method of the AutoResetEvent.

So I am using AutoResetEvent to set when these 2 events have finished. However, I seemed to be getting this error message
"Exception has been thrown by the target of an invocation."

And Inner exception
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index".

The exception usually fires when the registration success leaves scope.

Many thanks for any advice,


// 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();

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();
}
}