我想从我的主函数中调用线程实例。被调用的线程是我的abstact类Running中的重写函数。但是,我遇到了无法从静态函数调用非静态函数的问题。在C#中,main似乎总是一个静态函数。
代码段:
public class Program
{
static void Main(string[] args)
{
objectWriter = new WriterThread[ulMaxChannels];
threadWriter = new Thread[ulMaxChannels];
objectReader = new ReaderThread[ulMaxChannels];
threadReader = new Thread[ulMaxChannels];
objectReader[bChannelIndex] = new ReaderThread(Device, ref myParameters);
threadReader[bChannelIndex] = new Thread(new ThreadStart(objectReader[bChannelIndex].RunTransfers));
threadReader[bChannelIndex].Start();
public abstract class Running // WorkerThread
{
public void RunTransfers()
{
}
protected abstract
void TransferAsync
(byte bPipe, byte[] bBuffer, UInt32 ulPacketSize, ref UInt32 ulBytesTransferred, ref NativeOverlapped oOverlapped);
}
}
public class ReaderThread : Running
{
public ReaderThread(int Device, ref ReadWriteParameter Param)
: base(d3xxDevice, ref Param)
{
}
void TransferSync(byte bPipe, byte[] bBuffer, UInt32 ulPacketSize, ref UInt32 ulBytesTransferred)
{
}
void TransferAsync(byte bPipe, byte[] bBuffer, UInt32 ulPacketSize, ref UInt32 ulBytesTransferred, ref NativeOverlapped oOverlapped)
{
}
}
关联objectReader和线程阅读器时,我收到错误消息(从静态函数调用非静态成员。
我意识到必须创建一个程序对象:
Program p1 = new Program();
但是随后我不得不将p1添加到objectreader等中
p1.objectWriter = new WriterThread[ulMaxChannels];
p1.threadWriter = new Thread[ulMaxChannels];
p1.objectReader = new ReaderThread[ulMaxChannels];
p1.threadReader = new Thread[ulMaxChannels];
p1.objectReader[bChannelIndex] = new ReaderThread(Device, ref p1.myParameters);
p1.threadReader[bChannelIndex] = new Thread(new ThreadStart(p1.objectReader[bChannelIndex].RunTransfers));
p1.threadReader[bChannelIndex].Start();
第一个问题:这样安全吗? 第二个问题:是否有更方便的方法来使此工作正常进行?在我看来,这是使此工作变得困难的方法。我是否应该先将threadWriter和objectwriter排除在非静态函数之外?