If I have this thread:
Thread sendMessage = new Thread(new ThreadStart(timer.Start()));
Will the timer's Tick event be in the main thread or sendMessage thread?
Edit:
I have a queue, and I want every x milisecond timer to be ticked, and the program will deactivate the arrays from the queue, but this is my code:
Thread sendMessage = new Thread(new ThreadStart(startThreadTimer));
public Queue<Array> messageQueue = new Queue<Array>();
System.Threading.Timer timer;
private void startThreadTimer()
{
System.Threading.TimerCallback cb = new System.Threading.TimerCallback(checkIfQueue);
timer = new System.Threading.Timer(cb, null, 4000, 30);
}
private static void checkIfQueue(object obj)
{
}
and I cannot call the static method or not use the static field from checkIfQueue, and it must be static, what can I do?
Edit:
Here is the code that one of you sent me, I bound it to fit my purpose, will this work?
public ConcurrentQueue<Array> messageQueue = new ConcurrentQueue<Array>();
public void Example()
{
var thread = new Thread(
() =>
{
while (true)
{
Array array;
byte[] byteArray = {};
if (messageQueue.Count > 0)
{
messageQueue.TryDequeue(out array);
foreach (byte result in array)
{
byteArray[byteArray.Length] = result;
}
controllernp.Write(byteArray, 0, 100);
}
Thread.Sleep(30);
}
});
thread.IsBackground = true;
thread.Start();
}
source
share