Bizarre compile errors in Unity while Visual Studio will build project successfully
I'm writing a system to handle computation on a background thread, and I've been experiencing the weirdest compile-time errors I've seen in a while from Unity, and I'm convinced that Unity is getting it wrong. Visual Studio 2015 will build my code successfully without skipping a beat. I'm at my wit's end and I'm asking in desperation in case someone knows what trickery is necessary to get Unity to compile this.
Unity claims that a class property and a class method that are clearly defined are not in scope. The symbols in question are TaskSetTimeoutMS and ThreadProc(). I'm sure they exist and work fine because I was running tests using this code at runtime only hours ago and it has barely changed.
The compile errors I'm presented with are as follows:
Assets/Threading/Worker.cs(50,13): error CS0103: The name 'TaskSetTimeoutMS' does not exist in the current context.
Assets/Threading/Worker.cs(55,36): error CS0103: The name 'ThreadProc' does not exist in the current context.
Assets/Threading/Worker.cs(55,48): error CS1502: The best overloaded method match for 'System.Threading.Thread.Thread(System.Threading.ThreadStart)' has some invalid arguments.
Assets/Threading/Worker.cs(55,48): error CS1503: Argument '#1' cannot convert 'object' expression to type 'System.Threading.ThreadStart.
Errors 1 and 2 are the only ones that really matter. 3 and 4 are just side-effects of 2 and will be resolved if 2 goes away.
My code is as follows. I omitted the guts of ThreadProc() since they're long and irrelevant.
using System;
using System.Linq;
using System.Threading;
namespace Axon.Threading
{
public class Worker<TContext>
{
// Private
ManualResetEvent __beganWorkHandle;
ConcurrentQueue<Exception> __exceptionQueue;
bool __isStopping;
WaitHandle[] __taskDoneSignals;
ConcurrentQueue<Task<TContext>[]> __taskQueue;
int __taskSetTimeoutMS;
Thread __thread;
ManualResetEvent __waitHandle;
// Properties
public int TaskSetTimeoutMS
{
get { return __taskSetTimeoutMS; }
set
{
__taskSetTimeoutMS =
( value < 0 )
? Timeout.Infinite
: value;
}
}
// Computed Properties
public WaitHandle BeganWorkSignal
{
get { return __beganWorkHandle; }
}
public WaitHandle WaitHandle
{
get { return __waitHandle; }
}
public bool IsWorking
{
get { return __beganWorkHandle.WaitOne( 0 ); }
}
// Constructor
public Worker( int timeoutMS = Timeout.Infinite )
{
TaskSetTimeoutMS = timeoutMS;
__beganWorkHandle = new ManualResetEvent( false );
__exceptionQueue = new ConcurrentQueue<Exception>();
__taskQueue = new ConcurrentQueue<Task<TContext>[]>();
__waitHandle = new ManualResetEvent( false );
__thread = new Thread( ThreadProc );
__thread.Start();
}
// Thread Worker
void ThreadProc()
{
// ...
}
public WaitHandle EnqueueWork( Task<TContext>[] tasks )
{
// Reset the work finished signal before allowing ThreadProc() to run.
__waitHandle.Reset();
// Push the new tasks onto the task queue to be handled in parallel by ThreadProc().
// If the queue was previously empty, then the begin work signal should be set to start
// ThreadProc() up again.
__taskQueue.Enqueue( tasks );
if ( __taskQueue.Count == 1 )
{
__beganWorkHandle.Set();
}
// Return the wait handle to the caller as a convenience to save them having to look up
// the WaitHandle member separately.
return __waitHandle;
}
public Exception[] GetAndClearExceptions()
{
Exception[] exceptions = __exceptionQueue.CopyToArray();
__exceptionQueue.Clear();
return exceptions;
}
public void Stop()
{
__isStopping = true;
}
}
}
It should be fairly obvious that errors 1 and 2 are bogus. Both TaskSetTimeoutMS and ThreadProc() are defined as class members and are clearly in scope.
Visual Studio agrees with me, anyway. So what do I do about Unity?
I've tried:
creating a new project and copying my files over
deleting my project's temp files and .suo files to force a fresh build
every permutation of code order I can think of (as though that should even matter)
causing errors and then getting rid of them
cleaning and rebuilding in VS
None of this has helped.
And to top it off, if I remove all of the offending code from the file, Unity simply gives me the very same errors for the EnqueueWork() and Stop() symbols instead, while Visual Studio is still perfectly content to build the project.
I had a perfectly good working project only hours ago. Any idea what I can do with this? I'm having a rough day.
I'm running Unity 5.2.4f1.
Your answer
Follow this Question
Related Questions
Visual Studio 2019 Wont Compile After Unity 2019.3.12f1 update 4 Answers
Unable to load project - specified pathname not found compilation error 0 Answers
DOTS / ECS: Burst error BC1022: Accessing a managed array is not supported 0 Answers
UWP build crashes with "Windows.Gaming.Input.dll: Access denied" - what does it mean? 0 Answers
Missing type/namespaces do not allow me to playtest? 0 Answers