Help with WorkerThread

C

Cralis

Hi guys,

I am using a WorkerThread to do a background service call to check for
a new version availability.

But I need help with regards parameters.

So, I have a shared method that looks like this:

private void CheckFirmwareVersionOnServer(bool silentCheck)
{

Common.WriteLog("Firmware Check being started");
CommRequest cr = BuildServerCommsObject();
cr.CommandRequest = "CHECK_IS_LATEST_FIRMWARE";

PerformWebServiceAction(cr, silentCheck);

}

What's happening is, if silent check comes as TRUE, then I don't want
to show a result if there is no new version available. That's because
it was triggered automatically on startup. However, if the user select
a menu option that does a firmware check, then I pass FALSE, and it
shows the result whether or not there's a new version.

I then call PerformWebServiceAction, passing it an object I want to
send to the server, as well as the SilentCheck value:

private void PerformWebServiceAction(CommRequest commRequest, bool
silentCheck)
{
Common.WriteLog("Sending Web Service Request - " +
commRequest.CommandRequest);
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += PerformWebServiceActionDoWork;
worker.RunWorkerCompleted +=
PerformWebServiceActionComplete;
worker.RunWorkerAsync(commRequest);
}

My DoWork method is then called... but I can only pass it one
parameter. I pass the request object. But here's my problem. I'll
continue...

private static void PerformWebServiceActionDoWork(object sender,
DoWorkEventArgs e)
{
CommRequest commRequest = (CommRequest) e.Argument;
CommsService cs = new CommsService();
try
{
CommRequest reply = cs.Request(commRequest);
reply.CommunicationResult = true;
e.Result = reply;
}
catch (Exception ex)
{
commRequest.CommunicationResult = false;
commRequest.CommunicationError = ex.Message;
e.Result = commRequest;
}
}

On completion of the call, I do this:

private static void PerformWebServiceActionComplete(object sender,
RunWorkerCompletedEventArgs e)
{
CommRequest commRequest = (CommRequest)e.Result;

switch (commRequest.CommandRequest)
{
case "CHECK_IS_LATEST_FIRMWARE":
{
ProcessFirmwareReply(commRequest);
break;
}
default:
{
Common.WriteLog("\tError: Recieved a Service
Request reply, with a CommandRequest [" + commRequest.CommandRequest +
"] that is unhandled.");
break;
}
}

}

Problem is, at this point, I don't know if it was a silent check or
not. So, as you see, I call a method that handles the reply... very
basic, as I am in testing:

private static void ProcessFirmwareReply(CommRequest
commRequest)
{
MessageBox.Show(commRequest.ReplyDescription);
}

What I need to do here is check if it's a SilentCheck. If it is, I
must supress the messagebox, unless the commRequest has a flag that
says there IS a new firmware.

How can I get the SilentCheck flag to this stage?
 
M

mp

Cralis said:
Hi guys,

I am using a WorkerThread to do a background service call to check for
a new version availability.

But I need help with regards parameters.

So, I have a shared method that looks like this:

private void CheckFirmwareVersionOnServer(bool silentCheck)
{

Common.WriteLog("Firmware Check being started");
CommRequest cr = BuildServerCommsObject();
cr.CommandRequest = "CHECK_IS_LATEST_FIRMWARE";

PerformWebServiceAction(cr, silentCheck);

}
snip


Problem is, at this point, I don't know if it was a silent check or
not. So, as you see, I call a method that handles the reply... very
basic, as I am in testing:

private static void ProcessFirmwareReply(CommRequest
commRequest)
{
MessageBox.Show(commRequest.ReplyDescription);
}

What I need to do here is check if it's a SilentCheck. If it is, I
must supress the messagebox, unless the commRequest has a flag that
says there IS a new firmware.

How can I get the SilentCheck flag to this stage?

just an uneducated newby thought...
is CommRequest your object?
can you not add a .silent property to it?
sorry if that's just a stupid idea.
mark
 
C

Cralis

Thanks Pete and Mark.

I can add the property to my commRequest object, which is what I will
do. My only issue is that of design. The silent check property is for
the front end app, and hasn't got anything to do with the commRequest,
which is the object which is sent to the seb service. So I feel it's
adding extra 'weight' to the object. I was thinking the other option
was to have a private variable on the form, and just setting that,
which all methods could see.. but as I am in a thread, I guess that
can be changed while a call is in operation.

Thanks chaps!
 
C

Cralis

Thanks Pete.

I will attempt the private form field... which maybe has a state,
instead of a bool? So, maybe 'Sleeping', 'Silent Request Pending',
'Non Silent Request Pending'. Then, in my method that triggers the web
service, I can check the state. If it's Sleeping, I can start the
process, and set it to Silent Pending, or non Silent Pending,
depending on what I am doing. On completion, I force the state back to
Sleeping.

The only way that will fail is if an automatic (silent) check starts
at exactly the same time as a forced check - I think.

Does that seem the right way?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top