简体   繁体   English

如何从C#中的另一个线程调用一个线程?

[英]How to call a thread from another thread in C#?

I want to know how can I call a thread from another thread in C# Windows Form Application. 我想知道如何从C#Windows Form Application中的另一个线程调用线程。 I am developing the UI using TCP/IP communication. 我正在使用TCP / IP通信开发UI。 In my program, I send the data from Console application (client) to Windows Form Application(server) after receiving the data I want to plot it at UI on Picture Box. 在我的程序中,我收到要在Picture Box上的UI上绘制的数据后,将数据从控制台应用程序(客户端)发送到Windows窗体应用程序(服务器)。

I have created the two threads on the server first thread is the Listener thread (which listens to the client socket) and the second thread is the Handler thread (which handles data received from the client socket) I want to create the graphics plotting thread from the handler thread. 我在服务器上创建了两个线程,第一个线程是Listener线程(侦听客户端套接字),第二个线程是Handler线程(处理从客户端套接字接收的数据),我想从中创建图形绘制线程处理程序线程。

Currently, I am doing the graphics plotting within the handler thread, but I want to do it in a separate thread. 目前,我正在处理程序线程中进行图形绘制,但我想在单独的线程中进行图形绘制。 please guide me how can I do it? 请指导我该怎么做?

There's a million ways to skin this cat, but here's my suggestion for the simplest approach. 有上百万种方法可以给这只猫剥皮,但这是我对最简单方法的建议。 I assume you know how to create threads, etc. 我假设您知道如何创建线程等。

using System;
using System.Collections.Generic;
using System.Threading;

namespace foo
{
    class Packet
    {
        public double x;
        public double y;
        public double z;
    }

    public class Class1
    {
        Queue<Packet> qp;

        public Class1()
        {
            qp = new Queue<Packet>();
        }

        public void SendData(Packet p)
        {
            // prevent reader from accessing while we insert
            lock (qp)
            {
                qp.Enqueue(p);
            }
        }

        public void ProcessIncomingPackets()
        {
            Packet p;

            // or until we decide to stop
            while (true)
            {
                p = null;

                // prevent the writer from adding while we're reading and maybe removing an item
                lock (qp)
                {
                    if (0 < qp.Count)
                    {
                        p = qp.Dequeue();
                    }
                }

                // with lock released, do something if we have a packet
                if (null != p)
                {
                    // graph, etc.
                }

                // spin faster for better responsiveness and more wasted CPU
                Thread.Sleep(100); // milliseconds
            }
        }

    } // Class 1
} // namespace

There's also a million ways to optimize this, but also create really nasty bugs. 还有一百万种方法可以对此进行优化,但同时也会产生非常讨厌的错误。 So I suggest you do some reading on synchronization, threads, data structures for message passing, and those specific to C#. 因此,我建议您对同步,线程,用于消息传递的数据结构以及特定于C#的数据进行一些阅读。 A good computer science book on generic sync/threads generally provides a critical foundation for understanding the various platform/language specific implementations you'll encounter over the years. 一本关于通用同步/线程的优秀计算机科学书籍通常为理解您多年来会遇到的各种平台/语言特定实现提供了重要基础。 Testing any multithreaded code in tight loops is a good idea, especially if you can't prove the correctness of your synchronization directly. 在紧密循环中测试任何多线程代码是一个好主意,尤其是在无法直接证明同步正确性的情况下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM