Вот суть кода. Вам обязательно нужно будет внести изменения. Надеюсь, это поможет.
private static Queue<Dictionary<string, string>> messages;
....
{
...
AutoResetEvent ev = new AutoResetEvent(false);
...
Dictionary<string, string> msg1 = new Dictionary<string, string>();
msg1.Add("Id", "1");
msg1.Add("Fetch", "Song1");
Dictionary<string, string> msg2 = new Dictionary<string, string>();
msg2.Add("Id", "2");
msg2.Add("Fetch", "Song2");
messages.Enqueue(msg1);
messages.Enqueue(msg2);
ThreadPool.RegisterWaitForSingleObject(
ev,
new WaitOrTimerCallback(WaitProc),
messages,
5000,
false
);
// The main thread waits 10 seconds, to demonstrate the
// time-outs on the queued thread, and then signals.
Thread.Sleep(10000);
...
}
private static void WaitProc(object state, bool timedOut)
{
// The state object must be cast to the correct type, because the
// signature of the WaitOrTimerCallback delegate specifies type
// Object.
Queue<Dictionary<string, string>> dict = (Queue<Dictionary<string, string>>)state;
string cause = "TIMED OUT";
if (!timedOut)
{
cause = "SIGNALED";
//signaled to return. return without doing any work
return;
}
// timed out. now do the work
Dictionary<string, string> s1 = dict.Dequeue();
}
`