Question : Handling Asynchronous Timeouts

Hi All,

I'm developing a windows service in .Net to do some polling.  I was going to build it asynchronously to help performance and management of the threads, however I have a question about handling timeouts.

How do I handle a timeout using the begin invoke / end invoke?

I've seen the wait handles but not sure if that's what I want to be using.

Essentially the flow should look like:

call beginInvoke()
wait X seconds  (as defined by database)
handle timeout (if there is one)
call returns.

Links to coding examples or explanations would be great.

Answer : Handling Asynchronous Timeouts

In the button click event timer you have lines that start the ServiceHW and ServiceTimeOut methods on ThreadPool threads, and you are also calling RegisterWaitForSingleObject - but the WaitHandle's you're passing to RegisterWaitForSingleObject aren't associated with the ServiceHW or ServiceTimeOut methods.

Essentially, you're running the two worker threads, and the RegisterWaitForSingleObject calls are executing their methods every 5 seconds.  Basically, you're just using RegisterWaitForSingleObject as a timer that fires it's tick event every 5 seconds.

I think this is what you were trying to accomplish, though I don't think that you have any way to terminate the worker threads early - that is, even if their timed out callback fires, the thread is going to continue working.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        delegate void SetTextDelegate(string text, TextBox tb);

        public Form1()
        {
            InitializeComponent();
        }

        private void SetText(string text, TextBox tb)
        {
            if (tb.InvokeRequired)
            {
                this.Invoke(new SetTextDelegate(SetText),
                    new object[] { text, tb });
            }
            else
                tb.Text = text;
        }

        private void PauseFiveSeconds(AutoResetEvent are)
        {
            // Simulate database work that takes 5 seconds
            Thread.Sleep(5000);
            are.Set();
        }

        private void PauseEightSeconds(AutoResetEvent are)
        {
            // Simulate database work that takes 8 seconds
            Thread.Sleep(8000);
            are.Set();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "Working...";
            textBox2.Text = "Working...";

            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadOneProc));
            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadTwoProc));
        }

        private void ThreadOneProc(Object stateInfo)
        {
            AutoResetEvent wh = new AutoResetEvent(false);
            RegisteredWaitHandle handle = ThreadPool.RegisterWaitForSingleObject(
                wh,
                new WaitOrTimerCallback(ThreadOneTimedOut),
                null,
                6000,
                true);
            
            PauseFiveSeconds(wh);
            
            handle.Unregister(wh);
        }

        private void ThreadTwoProc(Object stateInfo)
        {
            AutoResetEvent wh = new AutoResetEvent(false);
            RegisteredWaitHandle handle = ThreadPool.RegisterWaitForSingleObject(
                wh,
                new WaitOrTimerCallback(ThreadTwoTimedOut),
                null,
                6000,
                true);

            PauseEightSeconds(wh);

            handle.Unregister(wh);
        }

        private void ThreadOneTimedOut(Object state, bool timedOut)
        {
            SetTextDelegate st = new SetTextDelegate(SetText);
            if (timedOut)
                st("Timed Out", textBox1);
            else
                st("Completed", textBox1);
        }

        private void ThreadTwoTimedOut(Object state, bool timedOut)
        {
            SetTextDelegate st = new SetTextDelegate(SetText);
            if (timedOut)
                st("Timed Out", textBox2);
            else
                st("Completed", textBox2);
        }
    }
}
Random Solutions  
 
programming4us programming4us