Would it be simple to change this code for thread

2019-08-22 18:00发布

问题:

I am trying to run the code from the article Thread Synchronized Queing

but getting compilation error:

The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)

My guess is that it is using generics and the change should be pretty trivial though I have not much experience with it.
How should I change this code?

I hope for the pretty simple change, otherwise just forget about it

The code from that article:

using System;
using System.Collections;
using System.Collections.Generic;//per comment by  @jam40jeff to answer
using System.Threading;

namespace QueueExample
{
  public class SyncQueue// per answer --> public class SyncQueue<T>
  {
    private WaitHandle[] handles = {
       new AutoResetEvent(false),
       new ManualResetEvent(false),
                                       };
    private Queue _q = new Queue();
    ////per comment by  @jam40jeff to answer, the above line should be changed to
    // private Queue<T> _q = new Queue<T>();

public int Count
{
  get
  {
    lock (_q)
    {
      return _q.Count;
    }
  }
}
public T Peek() //******error************************

{
  lock (_q)
  {
    if (_q.Count > 0)
      return _q.Peek();
  }
  return default(T);//******error************************
}

public void Enqueue(T element) //******error************************
{
  lock (_q)
  {
    _q.Enqueue(element);
    ((AutoResetEvent)handles[0]).Set();
  }
}

public T Dequeue(int timeout_milliseconds)//******error************************
{
  T element;//******error************************
  try
  {
    while (true)
    {
      if (WaitHandle.WaitAny(handles, timeout_milliseconds, true) == 0)
      {
        lock (_q)
        {
          if (_q.Count > 0)
          {
            element = _q.Dequeue();
            if (_q.Count > 0)
              ((AutoResetEvent)handles[0]).Set();
            return element;
          }
        }
      }
      else
      {
        return default(T);//******error************************
      }
    }
  }
  catch (Exception e)
  {
    return default(T);//******error************************
  }
}

public T Dequeue() //******error************************
{
  return Dequeue(-1);
}

public void Interrupt()
{
  ((ManualResetEvent)handles[1]).Set();
}
public void Uninterrupt()
{
  // for completeness, lets the queue be used again
  ((ManualResetEvent)handles[1]).Reset();
}

} }

Update:
After changing to

public class SyncQueue<T> 

according to answer, it was also necessary to change from:

return _q.Peek();

to

return (T)_q.Peek();

and from

element = _q.Dequeue();

to

element = (T)_q.Dequeue();

Update2:
Per comment of @jam40jeff to the answer:

  • "Change _q to be of type Queue<T>. Then you will need the using statement, but you won't need the casts to T"

my updates above were bad

回答1:

Maybe it's the mistake from author, class SyncQueue should be generic:

public class SyncQueue<T>

And to use generic, you also add one more using:

using System.Collections.Generic;

Then the code above should be fine.