Threading Questions

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();
        }
+3
source share
4 answers

X , ? , .

public class Example
{
  private ConcurrentQueue<Array> m_Queue = new ConcurrentQueue<Array>();

  public Example(int intervalMilliseconds)
  {
    var thread = new Thread(
      () =>
      {
        while (true)
        {
          Array array;
          while (m_Queue.TryDequeue(out array))
          {
            // Process the array here.
          }
          Thread.Sleep(intervalMilliseconds);
        }
      });
    thread.IsBackground = true;
    thread.Start();
  }

  public void Enqueue(Array array)
  {
    m_Queue.Enqueue(array);
  }
}

Update:

, . , .

if (messageQueue.Count > 0)
{
  messageQueue.TryDequeue(out array);
}

.

if (messageQueue.TryDequeue(out array)
{
}

TryDequeue false, , .

+1

. (System.Timers.Timer System.Threading.Timer), , ThreadPool Tick. "".

Windows Forms DispatcherTimer, , , , .

+5

, ..NET Framework ;

  • System.Threading.Timer = .
  • System.Windows.Forms.Timer = "UI".
  • System.Timer.Timer = .

, , , .

+3

, ; System.Threading.Timer System.Windows.Forms.Timer. , , ( thread, , , .)

, Windows Forms, , . .

, , , Tick . , , , . , (GUI) , . , , , , , . ( , .)

(If you didn’t receive it: the timer uses the internal window to receive the tick event. A window is created in the stream, and a message loop should be started for this stream. The timer will create a window and receive the Tick event. I hope this thread fires messageloop.)

+2
source

Source: https://habr.com/ru/post/1770961/


All Articles