I have a windows form application which is written in managed c#. In my application on login click i have to validate user credentials with a remote server through web service invocation. When there is internet connection(dongle) available there is no problem, but when internet connection is made off while calling web service System.NullReferenceException: Object reference not set to an instance of an object is being thrown. This scenario is every time reproducible.
I have used backgroungworker component from tool box and two eventhandler functions set from properties window of the same namely DoWork and RunWorkerCompleted.
when my background worker is executing web service i show progress(this.showdialog()) bar in a domodal manner from separate windows form and dispose that progress bar at RunWorkerCompleted function.
//Following is my code snippets:
// in Form1_designer.cs
BackgroundWorker bw = new BackgroundWorker();
bw.WorkerSupportsCancellation = true;
bw.DoWork +=
new DoWorkEventHandler(bw_DoWork);
bw.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
private btnClick_login(...)
{
bw.RunWorkerAsync();
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
if ((worker.CancellationPending == true))
{
e.Cancel = true;
}
else
{
e.Result = MyWebservice();
}
}
}
void MyWebService()
{
ActualWebService();
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if ((e.Cancelled == true))
{
TerminateApplication();
}
else if (!(e.Error == null))
{
loggedToFile(e.Error.Message);
}
else
{
ProgressForm.ActiveForm.Close();
}
}
Area of concern: While application is executing webiservice and internet dongle is removed then exception is being thrown by runtime though webservice call successfully is done with proper value of loss of internet connectivity
Kindly let me know if i am doing something wrong that leads to this situation
Thanks in advance.
System.NullReferenceException: Object reference not set to an instance of an object.
at LicenseManagerUI.NissanLicenseManagerForm.backgroundWorker1_RunWorkerCompleted(Object sender, RunWorkerCompletedEventArgs e)
at System.ComponentModel.BackgroundWorker.OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
at System.ComponentModel.BackgroundWorker.AsyncOperationCompleted(Object arg)