Wait events under the. NET Compact Framework Multithreading

Source: Internet
Author: User
Tags abstract compact thread

In wince or Windows Moblie development, a large amount of batch work is often submitted to worker Thread, and the interface process (UI thread) needs to be synchronized when the batch work completes, or if the process fails and the exception occurs. (PS: Sometimes two worker thread is synchronized, not UI thread, which is determined by the specific application.) It is often necessary to wait for the event. The wait events provided by the NET Framework are encapsulated inside the System.Threading.WaitHandle. But. The WaitHandle under the NET Compact Framework are not available. NET Framework, which provides only the WaitOne function (waiting for an event) in the waiting time. In fact, in general application, the UI process often waits for an event to suffice, below demonstrates the use of WaitOne.

Since WaitHandle is an abstract class (abstract class), the example uses its sub-class AutoResetEvent.

Defines field, both parent and child threads need access to the

private static AutoResetEvent autoEvent = new AutoResetEvent (false);

public bool Connect()
{
            //Do sth. eg make connections.
            ThreadPool.QueueUserWorkItem(
            new WaitCallback(CheckConnection), null);

            // Wait for work method to signal.
            if (autoEvent.WaitOne(5000, false))
            {
                return true;
            }
            else
            {
                return false;
            }
}

private void CheckConnection(Object stateInfo)
{
    while(true)
    {
            if (CheckConnection())
            {
                // Signal that work is finished.
                autoEvent.Set();
            }
    }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.