A hang on code?

  • Thread starter Thread starter Cor Ligthert [MVP]
  • Start date Start date
Hey all.

I need a improved code that waits for a execution before an another
precedure terminate processing.
Nowadays, I use the following code:


(...) 'executing some new threaded function...

'loop
While Not ThatFunctionTerminated
System.Windows.Forms.Application.DoEvents()
Syste.Threading.Thread.Sleep(1)
End While

(...) 'code to response to user...


But this code devoures some CPU processing.

There are some inbuilt method that waits for a procedure execution, with no
UI locking?

That loop runs in a server application, so that can be called many times at
same time. And the administrator need to be able to open the server
application and work with it, with no UI locking.


Cesar
 
Cesar,

I think so however I would try it with something as 50 milliseconds minium

Cor
 
that code is already the correct practice?

i think that the answer depends on the rest of your code


i would prefer an event driven aproach , if you are bussy with threading

regards

Michel Posseth [MCP]
 
Well, I guess that code was taking too many CPU execution. By your answer,
that code is already the correct practice?
 
I know this might not be very helpful right here and now,
but if you want to learn about multithreading in VB.NET,
go buy a copy of "Moving to VB.Net, 2nd Ed" by
Dan Appleman: http://tinyurl.com/9js8t

Chapter 7 (around 50-60 pages) is dedicated to multithreading.

Sure, you can find the same information online, but
this chapter goes to great lengths explaining the pitfalls
of using multithreading in VB.Net.

I am certainly going to re-read (and re-read) it if I
start using it.

Regards,

Joergen Bech
 
Hi,

Your code is similar to what I do.

However... If you are seeing the Task Manager go to 100% (which IS NOT a
hang -- other applications still get their full portion of processing time),
there is something else going on. The Sleep should keep your app from going
to 100%, so perhaps there is some other code in your app that is consuming
the time?

Dick

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 4th
Edition ISBN 1-890422-28-2 (391 pages) published July 2004. See
www.mabry.com/vbpgser4 to order.
 
If you can get a handle to the application you're waiting on, you can have
the OS block your thread until that handle signals using the
WaitForSingleObject API. You'll need to do this is in a worker thread as
all Wait Functions block their thread.

Mike Ober.
 
Actually you can - use the EnumWindows API to find your process. If it's
not running, the EnumWindows API won't find it. If it is, you'll get either
the hWND or the Handle to the process. Once you have that, you can wait for
it.

Mike Ober.

Cesar Ronchese said:
Unfortunatelly, I cant use event... the server program controls when the
task was executed and dispatch it to client, which is already waiting the
for response.





m.posseth said:
that code is already the correct practice?

i think that the answer depends on the rest of your code


i would prefer an event driven aproach , if you are bussy with threading

regards

Michel Posseth [MCP]



Cesar Ronchese said:
Well, I guess that code was taking too many CPU execution. By your
answer, that code is already the correct practice?




Cesar,

1 milliseconds is not so much

:-))

Cor

"Cesar Ronchese" <info||carsoftnet.com.br> schreef in bericht
Hey all.

I need a improved code that waits for a execution before an another
precedure terminate processing.
Nowadays, I use the following code:


(...) 'executing some new threaded function...

'loop
While Not ThatFunctionTerminated
System.Windows.Forms.Application.DoEvents()
Syste.Threading.Thread.Sleep(1)
End While

(...) 'code to response to user...


But this code devoures some CPU processing.

There are some inbuilt method that waits for a procedure execution,
with no UI locking?

That loop runs in a server application, so that can be called many
times at same time. And the administrator need to be able to open the
server application and work with it, with no UI locking.


Cesar
 
Unfortunatelly, I cant use event... the server program controls when the
task was executed and dispatch it to client, which is already waiting the
for response.





m.posseth said:
that code is already the correct practice?

i think that the answer depends on the rest of your code


i would prefer an event driven aproach , if you are bussy with threading

regards

Michel Posseth [MCP]



Cesar Ronchese said:
Well, I guess that code was taking too many CPU execution. By your
answer, that code is already the correct practice?
 
I can change it, sure. I was thinking really there was another improved form
to do that :-D

Thanks
Cesar
 
Not going to 100%, thanks to the Sleep() method, as you said too. What I
meant about taking CPU processing (did not noticed it was important, anyways
is my fail to explain) is because I did some tests breaking package, and the
time to terminate the job is a horror comparing direct execution. As I said,
I would like to know if there are other form to do that.

As you guys do this way also, so I'll keep it.

Thanks
Cesar
 
Hmmm.. really, I already have that book (were my first .Net book I bought).
Although that's applicable to .Net 2002, it still can be useful. I'll read
that chapter again. :)

Thanks for the reminder
 
I can give a try on it (when I was programming in VB6 I noticed it freeze
the application).
I'll see if works well in .Net, multithreading it.

Thx
Cesar
 

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

Back
Top