Claude,

Greg is right, now looking at the code below.  It is possible to perform a loop 
on the ui thread and call Application.DoEvents, but its not ideal, especially 
if some action that takes place in the timeslice the UI thread gets involves 
blocking - this would hold up your loop that is trying to send and receive.

I agree 100% with Greg - put this logic into an object and use the Async 
Pattern - Jeffrey Richter has a good explanation on MSDN.  You are free to use 
a wealth of techniques for dealing with updating the ui with this approach.  
You could also create yourself an active object using a thread, but whatever 
approach you take, it seems clear it has to be performed off the ui thread.

You mentioned some synchronous aspects, which is true in the sense of your 
application protocol you are using (PrintRequrest/WaitResponse/....), and while 
this might not seem asynchronous, it can certainly be seen as async when 
considered to be running on a separate thread of execution, given all other 
threads can still make progress, specifically your ui thread.  

There are different things you could do with the async pattern, from simply 
getting the whole code below to run in serial from start to finish, but on a 
different thread, to breaking this logic up differently.  However, which ever 
way, personally I wouldnt bother pumping the ui message queue myself and just 
let .Net deal with it while I run my code on a distinctly separate thread, with 
no need to call DoEvents.

Best regards,

Nick.

> -----Original Message-----
> From: [EMAIL PROTECTED]
> Sent: Fri, 11 Jan 2008 18:00:45 -0500
> To: advanced-dotnet@discuss.develop.com
> Subject: Re: [ADVANCED-DOTNET] How to know if the current thread is the
> UI thread or a worker thread
> 
> I want to be able to do the following :
> 
> 
>         PrintRequest();
>         WaitResponse();
>         PrintStart();
>         WaitResponse();
>         int packets = 0;
>         while (data.length > 0)
>         {
>                 if (packets >= 27)
>                 {
>                         FlushBuffer(packes);
>                         WaitResponse();
>                         PrintStart();
>                         WaitResponse();
>                         packets = 0;
>                 }
> 
>                 PrintData();
>                 WaitResponse();
> 
>                 packets++
>         }
>         PrintEnd(packets)
>         WaitResponse();
> 
> 
> 
> -----Message d'origine-----
> De : Discussion of advanced .NET topics.
> [mailto:[EMAIL PROTECTED] la part de Greg Young
> Envoyé : Friday, January 11, 2008 01:17
> À : ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
> Objet : Re: [ADVANCED-DOTNET] How to know if the current thread is the
> UI thread or a worker thread
> 
> 
> this is absolutely a task that can be done async, many applications
> handle similar "seemingly" blocking synchronous tasks in an
> asynchronous way
> 
> On Jan 10, 2008 8:14 PM, Claude Petit <[EMAIL PROTECTED]> wrote:
>> I have to wait many responses from an electronic device, and have many
>> commands to transmit before the command (method) really ends. Yes, I
>> need
> to
>> block.
>> 
>> -----Message d'origine-----
>> De : Discussion of advanced .NET topics.
>> [mailto:[EMAIL PROTECTED] la part de Greg Young
>> Envoyé : Thursday, January 10, 2008 23:03
>> À : ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
>> Objet : Re: [ADVANCED-DOTNET] How to know if the current thread is the
>> UI thread or a worker thread
>> 
>> 
>> If you are running into this you probably need to re-think how you are
>> doing things. It would seem to me that you could redo this pretty
>> simply to be a non-blocking call which would remove this requirement.
>> A common methodology for doing this would be to use the Async Pattern.
>> 
>> Cheers,
>> 
>> Greg
>> 
>> On Jan 10, 2008 6:50 PM, Claude Petit <[EMAIL PROTECTED]> wrote:
>>> Hi,
>>> 
>>> I want to know when to call Application.DoEvents when I'm waiting for
>>> something. The calling thread can be either the ui thread or a worker
>>> thread. The ui thread must be able to handle its events, but the worker
>>> thread can be blocked. Thank you.
>>> 
>>> Claude Petit
>>> 
>>> ===================================
>>> This list is hosted by DevelopMentor(R)  http://www.develop.com
>>> 
>>> View archives and manage your subscription(s) at
>> http://discuss.develop.com
>>> 
>> 
>> 
>> 
>> --
>> Studying for the Turing test
>> 
>> ===================================
>> This list is hosted by DevelopMentor(R)  http://www.develop.com
>> 
>> View archives and manage your subscription(s) at
> http://discuss.develop.com
>> 
>> ===================================
>> This list is hosted by DevelopMentor(R)  http://www.develop.com
>> 
>> 
>> View archives and manage your subscription(s) at
> http://discuss.develop.com
>> 
> 
> 
> 
> --
> Studying for the Turing test
> 
> ===================================
> This list is hosted by DevelopMentor®  http://www.develop.com
> 
> View archives and manage your subscription(s) at
> http://discuss.develop.com
> 
> ===================================
> This list is hosted by DevelopMentor®  http://www.develop.com
> 
> View archives and manage your subscription(s) at
> http://discuss.develop.com

===================================
This list is hosted by DevelopMentor�  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to