.NET Framework Bookmark and Share   
 index > .NET Base Class Library > Thread block and awake
 

Thread block and awake

Hi, thread question:

   public void FinishReception()
        {

            if(!this.Visible)
                this thread wait

                MethodInvoker mi = new MethodInvoker(Method that will update UI);
                this.BeginInvoke(mi);
            }

        }
so i open a new form with a control that imediately will be wait for a file transfer that is tranfering on another thread and show the document and set some fields according to the file data. when the file arrives the FinishReception() method runs.

When the file arrives after thecontrol is visible i've got no problem, but when the file arrives before the control is visible i can't use the invoke.

How do i stop the thread that is running FinishReception() and how and when do i awake it ?

Thanks
  • Moved byKarel ZikmundMSFTWednesday, September 16, 2009 6:24 AMNot 4.0 specific question (From:.NET Framework 4 Application Compatibility - Beta 1)
  •  
vasco1610
The problem is probably caused by when you run the method, not by the time it takes to read the file.

Use the OnShown method instead of OnLoad to run the thread will guarantee that the control is ready.

Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2
Seems it is a race condition issue, you may try to use ManualResetEvent to do thread synchronization:
1. Add a ManualResetEvent.

private ManualResetEvent mr = new ManualResetEvent(false);

2. Refine FinishReception() method:

public void FinishReception()
{
if(!this.Visible)
{
//Current thread will be blocked until mr.Set() is called.
mr.WaitOne();
}

MethodInvoker mi = new MethodInvoker(Method that will update UI);
this.BeginInvoke(mi);
}


3. Call mr.Set() in the end of main form's OnShown() method (or other initiating methods).

Thanks,
Eric

Please remember to mark helpful replies as answers and unmark them if they provide no help.
eryang
The problem is probably caused by when you run the method, not by the time it takes to read the file.

Use the OnShown method instead of OnLoad to run the thread will guarantee that the control is ready.

Mark the best replies as answers. "Fooling computers since 1971."
Rudedog2
But we may save some time if start to read the file before the form shown completely, or am i missed anything?

Thanks,
Eric

Please remember to mark helpful replies as answers and unmark them if they provide no help.
eryang
Eric's code has an inherent race condition. Follow Rudy's advice, wait until OnShown() to start the file transfer. You must also make sure to either terminate the thread or cancel the FormClosing event when the user tries to close the form when the transfer isn't completed yet.

Hans Passant.
nobugz

Thanks for your kind remainder, nobugz.
I re-checked my code and found that Onload() method is not proper place for mr.Set(), because if current thread swith to the wait thread immediate after UI thread's mr.Set(), the problem in OP's code will happen again.
So, I move mr.Set() into form's OnShown() method, it can make sure that the wait thread will be actived only after form is shown completely.
By the way, I don't think it is a good way to start file transfer after form's OnShown(), it will make user waiting for a long time if the the file transfer is a time-consuming operation.

Thanks,
Eric


Please remember to mark helpful replies as answers and unmark them if they provide no help.
eryang

You can use google to search for other answers

Custom Search

More Threads

• How to tell: "exit and run another .exe"?
• What is the best practice to get the resource file translated into other languages?
• cannot find al.exe in SDK
• enum from string[]
• Do we Have Enough Base Class Library For Digital Certificate Management?
• Can I install Visual Studio 2005 on a machine with Visual Studio 2003?
• TextBox Control Formatting
• Anonymous Method on a DropDown Event in a GridView
• Thread Sychnronization issues
• Documentation: SynchronizationContext and synchronization domains?