Thanks Jason, I have implimented threading, so my GUI thread is free
while the WS does it's thing. Here's my current implimentation:
private void PerformWebServiceAction(CommRequest commRequest)
{
Common.WriteLog("Sending Web Service Request - " +
commRequest.CommandRequest);
if (_webServiceState == WebServiceState.SLEEPING)
{
tsWsStatus.ForeColor = Color.Red;
tsWsStatus.Text = "Communicating... >>";
_webServiceState = WebServiceState.REQUEST_PENDING;
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += PerformWebServiceActionDoWork;
worker.RunWorkerCompleted +=
PerformWebServiceActionComplete;
worker.RunWorkerAsync(commRequest);
}
else
{
Common.WriteLog("Cannot initiate Web Service call as
there is a result pending.");
if (commRequest.GetParameterValue("SilentCheck") ==
"0")
{
MessageBox.Show(
"Unable to do a " + commRequest.CommandRequest
+ " at the moment, as there is a request already in progress,\r\n\r
\nPlease try again in a few moments.", commRequest.CommandRequest,
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
The actual work is then done in this method in the worker thread:
private void PerformWebServiceActionDoWork(object sender,
DoWorkEventArgs e)
{
CommRequest commRequest = (CommRequest)e.Argument;
CommsService cs = new CommsService();
cs.Timeout = 15000;
try
{
CommRequest reply = cs.Request(commRequest);
reply.CommunicationResult = true;
e.Result = reply;
}
catch (Exception ex)
{
Common.WriteLog("\t\tERROR: Failed to make WebService
call. " + ex.Message);
_webServiceState = WebServiceState.SLEEPING;
commRequest.CommunicationResult = false;
commRequest.CommunicationError = ex.Message;
e.Result = commRequest;
}
}
And then when the WS completes, this method is used to handle the
reply:
private void PerformWebServiceActionComplete(object sender,
RunWorkerCompletedEventArgs e)
{
_webServiceState = WebServiceState.SLEEPING;
tsWsStatus.ForeColor = Color.Black;
tsWsStatus.Text = "Inactive";
CommRequest commRequest = (CommRequest)e.Result;
switch (commRequest.CommandRequest)
{
case "CHECK_IS_LATEST_FIRMWARE":
{
ProcessFirmwareReply(commRequest);
break;
}
case "REQUEST_NEW_DEVICE_UID":
{
ProcessNewDeviceUidRequest(commRequest);
break;
}
case "REGISTER_NEW_DEVICE_UID":
{
ProcessDeviceRegistration(commRequest);
break;
}
case "DOWNLOAD_FIRMWARE":
{
ProcessFirmwareDownload(commRequest);
break;
}
case "CHECK_IS_LATEST_SOFTWARE":
{
ProcessSoftwareReply(commRequest);
break;
}
default:
{
Common.WriteLog("\tError: Recieved a Service
Request reply, with a CommandRequest [" + commRequest.CommandRequest +
"] that is unhandled.");
break;
}
}
}
That is all working 100%
But, I'd like the status bar to change when the reply starts, as when
I do a request, the request may take 2 seconds, and the reply takes
between 2 seconds and 10 seconds, depending on what it's doing. So I'd
like a visual indication as to what state the WS request is in.