Click here to get back home

Asynch wininet - Threaded or not?

 HomeNewsGroups | Search | About
 microsoft.public.smartphone.developer    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Asynch wininet - Threaded or not? Fred 02-01-2006
Posted by Fred on February 2, 2006, 2:48 pm
Please log in for more thread options

>
> > Fred,
> >
> > Let's discuss what your old book says:
> >
> > "The async mode is most suited to applications which must be single
> threaded
> > and stay responsive to the end user and in extreme performance and
memory
> > sensitive situations"
> >
> > This is technically true, but don't be misled by it!
> >
> > If an application must be single threaded, then synchronous mode by
> > definition will prevent it from returning to the message loop until it
is
> > done. However, there is no such application in practice. Who's stopping
> you
> > from using multiple threads? Nobody! Only your own lack of knowledge
about
> > multithreaded programming, perhaps.
>
> Yes as far as synchronous/asynchronous Wininet and multithreading is
> concerned.
> I have written a pretty complicated prog with a thread pool and
synchronous
> Wininet and it works - but I'm still not happy about cancelling threads
when
> we have a timed-out block.
>
> > The rest of it is presumably talking about the expense of creating a
> thread.
> > It takes a little time and it allocates some memory (the largest part of
> > which is the stack). However, these are not really relevant issues for
> most
> > applications. You're not going to be downloading thousands of web pages
at
> > once are you?
>
> What is the extra complexity involved in writing asynch wininet?
>
> MS's asyncdemo doesn't look too bad
>
>
http://msdn.microsoft.com/library/en-us/wininet/wininet/calling_wininet_functions_asynchronously.asp
>

Incidentally it also uses threads!!!!!!





Posted by Fred on February 2, 2006, 2:38 pm
Please log in for more thread options

> Fred,
>
> As far as I know, INTERNET_OPTION_CONNECT_TIMEOUT does work. Rather than
> base the entire design of your software on the premise that it does not, I
> would go into more detail about that here and someone should be able to
help
> you. You might also closely review the documentation regarding the use of
> this constant.
>
> It does not affect performance. It's simply easier to use WinInet
> synchronously rather than asynchronously. And creating a thread is almost
> trivial.
>
> To terminate a synchronous operation, all you need to do is close the
handle
> on which it acts or one if its parents.
>

Could you be explicit with a code snippet as I remain confused given the MS
support docs

http://support.microsoft.com/kb/176420/EN-US/
http://support.microsoft.com/kb/224318/EN-US/

Say I have several threads carrying out their own requests. One of them is
taking too long.
and I wish to terminate that request yet leave the others untouched.

How do i do it?

Thanks





Posted by Scherbina Vladimir on February 2, 2006, 3:24 pm
Please log in for more thread options
Associate handle of thread with a list of HINTERNET handles that are using
in thread. When you see that thread is working "too long", call
InternetCloseHandle on those HINTERNET handles. This is the simplest
solution.

--
Vladimir

>
>> Fred,
>>
>> As far as I know, INTERNET_OPTION_CONNECT_TIMEOUT does work. Rather than
>> base the entire design of your software on the premise that it does not,
>> I
>> would go into more detail about that here and someone should be able to
> help
>> you. You might also closely review the documentation regarding the use of
>> this constant.
>>
>> It does not affect performance. It's simply easier to use WinInet
>> synchronously rather than asynchronously. And creating a thread is almost
>> trivial.
>>
>> To terminate a synchronous operation, all you need to do is close the
> handle
>> on which it acts or one if its parents.
>>
>
> Could you be explicit with a code snippet as I remain confused given the
> MS
> support docs
>
> http://support.microsoft.com/kb/176420/EN-US/
> http://support.microsoft.com/kb/224318/EN-US/
>
> Say I have several threads carrying out their own requests. One of them is
> taking too long.
> and I wish to terminate that request yet leave the others untouched.
>
> How do i do it?
>
> Thanks
>
>
>
>



Posted by Fred on February 3, 2006, 7:05 am
Please log in for more thread options

> Associate handle of thread with a list of HINTERNET handles that are using
> in thread. When you see that thread is working "too long", call
> InternetCloseHandle on those HINTERNET handles. This is the simplest
> solution.

Hi,

The demo http://support.microsoft.com/kb/224318/EN-US/

uses the handle for InternetOpen () to close the thread if the thread is
hanging. So if I have multiple connections (threads) from the one
InternetOpen () (which is the recommended technique) then any wininet
activity must surely be terminated on *all* these threads - which is not
what I want.

Now documentation states:

"The application can make any number of calls to InternetOpen, though a
single call is normally sufficient. "

So to stop one hanging thread then I would have to have a separate
InternetOpen for each thread and then a separate Internetconnect() too -
what is the overhead for this? Surely it is more than marginal - and don't
forget I will be porting to SmartPhone which has nowhere near the horsepower
of a PC.

These above concerns are the issues which prompted my initial post.

So, how do you handle hanging/blocking threads? Terminating them all when
only one is blocking, say on an InternetReadFile() cannot be the ideal
solution.

Further can you actual terminate on any blocking wininet function, say if
InternetReadFile() is blocking or does it only work for Internetconnect?

Thanks




Posted by Scherbina Vladimir on February 3, 2006, 7:59 am
Please log in for more thread options
Hello, Fred.

>
> The demo http://support.microsoft.com/kb/224318/EN-US/
>
> uses the handle for InternetOpen () to close the thread if the thread is
> hanging. So if I have multiple connections (threads) from the one
> InternetOpen () (which is the recommended technique) then any wininet
> activity must surely be terminated on *all* these threads - which is not
> what I want.
>
> Now documentation states:
>
> "The application can make any number of calls to InternetOpen, though a
> single call is normally sufficient. "
>
> So to stop one hanging thread then I would have to have a separate
> InternetOpen for each thread and then a separate Internetconnect() too -
> what is the overhead for this? Surely it is more than marginal - and don't
> forget I will be porting to SmartPhone which has nowhere near the
> horsepower
> of a PC.
>
> These above concerns are the issues which prompted my initial post.
>
> So, how do you handle hanging/blocking threads? Terminating them all when
> only one is blocking, say on an InternetReadFile() cannot be the ideal
> solution.
>
> Further can you actual terminate on any blocking wininet function, say if
> InternetReadFile() is blocking or does it only work for Internetconnect?
>
> Thanks

There is no need to make an InternetOpen in all threads - this is a really
overhead. You may call InternetOpen once and use returned handle in all
threads.

Each thread (or thread from the pool) makes InternetConnect using handle
returned by InternetOpen. Most likely your thread would not be blocked by
call of InternetConnect, it might hung when you will make call to
HttpOpenRequest / HttpSendRequst, so to cancel blocking of thread you should
close handle of request(or whatever) it made. Microsofts examples closes
handle returned by InternetOpen, but you may cancel execution closing handle
returned by HttpOpenRequest or simular function.

So, once more:

1. Locate where your thread may hung (InternetReadFile, HttpSendRequest,
etc)
2. Associate HINTERNET handles open by thread with thread's handle.
3. Mark position *what* wininet function is executed in each thread.
4. When thread is hung, read the position - you know what function is
executed, and the handle it uses, simply close it.

--
Vladimir



Similar ThreadsPosted
Re: Asynch wininet - Threaded or not? February 2, 2006, 2:45 pm
SSL with WinINet April 16, 2008, 4:30 pm
WinInet problem August 1, 2005, 1:44 am
Can wininet functions trigger CM to auto-establish a GPRS connecti June 11, 2005, 10:03 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap