We help IT Professionals succeed at work.

Multithreaded Web Service - ASP.NET C# 2.0

562 Views
Last Modified: 2013-11-07
Hello Experts,
I would like to write a web service which will ping some number of computers, and will do it asynchronously (using threads).

How can I notify the calling page that the web service finished its work and data can be retrieved? (on winforms I did it with event...)

Please provide some example code....

Comment
Watch Question

Author

Commented:
Thank you answer_me.
This is a great article, but it doesn't fully covers my issue (or maybe i'm not fully understand it).

In the example the web function MyWebMethod called Asynchronously and when finished the callback function called.
This is fine, and kind of what I need, but not exactly.

In my case the MyWebMethod calls some threads on the server, and when the thread finished a server side callback function called, lets call it ServerCallbackMethod.

I would like the ServerCallbackMethod to triger the CallbackSampleMethod.

I hope that using this example my question is a little bit more clear.

Thanks again.
In your case where is CallbackSampleMethod located

Author

Commented:
CallbackSampleMethod is on the client page.
ServerCallbackMethod is on the server.

Author

Commented:
Sorry.
CallbackSampleMethod is on the server (ofcource!) but it is a part of client app.

ServerCallbackMethod  is the callback function of the web service
But why do you need a call back function on web service.

Author

Commented:
This is very simple...
In my organization all the computers called username_something. The something part can vary but there are final amount of options.
I need to ping all the options in order to find the computer in the network.
Right now I have a client app which does this and works great with threads .

The web service will do the same thing. Receive the username and will ping all the available options.

Is it clear now?

(by the way, tried to find better way, but all the other options are not available in my org...)
Why don't you call the pinging api of your web service asynchronously. In that case the call back function will be present in your asp page.

Author

Commented:
Because I don't want the client page to know the logic...
All it has to know is the username and to ask the web service for the machine name.
Why would I need the web service if I have all the logic in client page?
And if someone else wants to use the same functionality?
There is some confusion here. What the client should not know
Are you talking about pinging logic? Or some logic written in call back function is confidential.

Author

Commented:
I guess I didn't explain myself well...

As I said before:
<---
In my organization all the computers called username_something. The something part can vary but there are final amount of options.
I need to ping all the options in order to find the computer in the network.
--->

This part should be done by webservice.  Nothing confidential...

The consumer of the webservice should only get a username and pass it to the webservice in order to receive the machine name.

I know already about at least 3 different consumers who can use this functionality.

Hope it is clearer now.
 
Do You mean to say
1. You call web service pinging api
2. once pinging is done you get user name pass on the web server.
3. You pass username pass to webservice.
4 you get the machine name on the webserver.

Author

Commented:
NO!


Webservice knows how to take a username and try pinging all the possible options (multithreaded). The output of the webservice is a string array of IPs and machine names who answered the ping.

Consumer gets the username and calls the webcervice.
Webservice does all the pinging and returns with the string array...

Author

Commented:
Hello answer_me:
Do you have any direction? Did you give up on me?
I know I may not be very clear but we can work on that.
I just want to know you are working with me.....

Thanks
Sorry I was stuck with some work.

I suppose there should be no confusion regarding implementation here. Steps should be:

1. Call the web service method for pinging in asyn mode. from the web server i.e. consumer. Set some callback function
2. The web service method pings all the machines and prepares the string array for all those machines which are online and return the array.
3.  In the call back function collect the return array.

Thats it

Author

Commented:
Come on, we have already been here....

The webservice method (will call it PingMethod) starts N threads on webservice and finishes, right?
How do you know when all the threads are finished? You have a callback function PingCallback (this is also part of webservice).

So, we already established the understanding that the thread will finish long after the PingMethod finishes.

Now, back to my issue.

If I try to retrieve the array as soon as the PingMethod finishes -  I'll get an empty array, right?
So, I have (somehow) to wait for the PingCallback to finish.
This is my issue, because when you call a method on webservice you can't (as far as I understand) wait for another method, only the one you called.

You don't need async callback mechanism on web service for this. I'll suggest you two things here.
1. Either you ping the machines synchronously and return the array.
2. You spawn multiple threads from web service method for pinging and wait for threads to complete. Whatever data you collect Return after all threads are completed.

Author

Commented:
You right, I don't.

All I want to do is to implement the:
<---
You spawn multiple threads from web service method for pinging and wait for threads to complete. Whatever data you collect Return after all threads are completed.
--->

All the essence of my question is how to know when my Webservice complites all the threads.

Can you give me the answer for this?
Please...

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Great!
Thanks.

I will have to play with this, but I believe this is what I was looking for (kind of....)/

Thank you again.

Author

Commented:
Took some time to explain my issue, but eventually it worked :)

Thank you for your help

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.