Question about no response of main Form

C

campos

Hi all,

I ran into a headache problem. I have a windows form with a progress
bar on it. Then I new a thread to do calculation for a long time. I
want the progress bar to show the calculation progress. So I use a
shared variable in calculation thread to allow main thread read it
periodically in order to show the progress. Quesion comes out. If I
just wait and don't make any operation during the whole calculation,
the main form is OK, with progress bar increased step by step. But If
I click other application or just drag the form, main form will have
no response until the calculation finished. At first I think that
maybe the calculation is too tight to be a circle. So I add several
sleep() in it. But this method is of no use. I have no idea this time.
Anybody can help me? Thanks in advance!
 
L

Larry Smith

I ran into a headache problem. I have a windows form with a progress
bar on it. Then I new a thread to do calculation for a long time. I
want the progress bar to show the calculation progress. So I use a
shared variable in calculation thread to allow main thread read it
periodically in order to show the progress. Quesion comes out. If I
just wait and don't make any operation during the whole calculation,
the main form is OK, with progress bar increased step by step. But If
I click other application or just drag the form, main form will have
no response until the calculation finished. At first I think that
maybe the calculation is too tight to be a circle. So I add several
sleep() in it. But this method is of no use. I have no idea this time.
Anybody can help me? Thanks in advance!

Does the main thread launch your worker thread and then immediately enter
some loop to periodically check the shared variable? It certainly appears
that way and if so then it's not correct. You can Google for "Windows
message loops" to understand why but to correct the problem you may want to
have a look at the "BackgroundWorker" class. It's designed to handle exactly
what you're trying to do although you can still resort to other conventional
means if you want (once you read up on Windows message loops but why bother
when "BackgroundWorker" makes life easier).
 
C

campos

Does the main thread launch your worker thread and then immediately enter
some loop to periodically check the shared variable? It certainly appears
that way and if so then it's not correct. You can Google for "Windows
message loops" to understand why but to correct the problem you may want to
have a look at the "BackgroundWorker" class. It's designed to handle exactly
what you're trying to do although you can still resort to other conventional
means if you want (once you read up on Windows message loops but why bother
when "BackgroundWorker" makes life easier).

Hi Larry,

BackgroundWorker, really cool. That works!

I think over for my periodical checking status in main thread, that
should be blocked in main form. But what about using a timer, I
haven't tried that. My intuition is that timer maybe OK. Anyway, I'll
spend my time in studying Windows message loops after finishing. Thank
you very much!
 

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