本文介绍了多线程应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部光晕

我已经在这上面b了一段时间.希望有人能提供一些建议.

I've been bashing my head on this one for a while now. Hope somebody can give some advise.

我们是一家工厂自动化公司,我们使用Visual Studio从设备中获取信息,并根据各种控制和状态字直观地显示状态并将数据发送回去.

We are a factory automation company, we use visual studio to get information from devices and visually display the status and send data back depending on the various control and status words.

现在,对于该应用程序,我们得到了一个PLC,该PLC正在从各种类型的设备(例如,设备)收集数据. 10 x检查摄像机连接到单个PLC,检查摄像机根据状态更新PLC中的值.例如.每个摄像机有10个寄存器 PLC正在读取的内容.

Now for this application we got a PLC that is collecting data from various types of equipment, eg. 10 x Inspection camera's are connected to a single PLC, the inspection camera's updates values in the PLC depending on the state. eg. each camera got 10 registers which the PLC is reading. 

我们使用Visual Studio从PLC收集100个寄存器,例如摄像机1(寄存器0-9)和摄像机2(寄存器10-11)等.

We use Visual studio to collect the 100 registers from the PLC, let say for instance camera 1 (Register 0 - 9) and camera 2 (Register 10 - 11) etc....

在Visual Studio中,我们获得了100个寄存器并监视每个摄像机的状态,摄像机的状态在摄像机1的寄存器9中,在摄像机2的寄存器11中,等等...

In visual studio we get the 100 registers and monitoring each camera state, the state of the camera's is in register 9 for camera 1 and register 11 for camera 2 etc...

当这些寄存器中的任何一位变为高电平时,我们的代码需要执行一些方法以完成工作,然后通知相机的寄存器0,11等中的PLC.

When any of the bits in these registers goes high our code needs to execute some methods for work to be done and then notify the PLC in register 0,11 etc... for the camera.

但是有些过程可能会同时完成.

But there is processes that might be done simultaneously.

现在我要提出的问题是,我们希望在代码上设置10个线程,以确保可以同时执行工作.

Now for my question, we are looking to setup 10 threads on the code to ensure the work can be executed at the same time. 

我在下面尝试使用一个简单的测试程序,但是当我们更新按钮并执行新线程时,无法控制或查看哪些线程处于活动状态.

I've tried using a simple test program below, but when we update the button and execute a new thread there is no way to control or to see which threads are active.

下面是一个简单的代码,通过使用streamwrite来测试流.但是进入线程后,它将连续执行.

Below a simple code to test the flow by making use of a streamwrite. but when going into the thread it will execute continuously.

    public partial class Form1 : Form
    {
        bool butn1 = false;
        bool butn2 = false;
        bool butn3 = false;
        bool butn4 = false;
        public Form1()
        {
            InitializeComponent();
        }

        private void but1_Click(object sender, EventArgs e)
        {           
            if (butn1)
            {
                butn1 = false;                
            }
            else
            {
                butn1 = true; 
            }
            txtd1.Text = butn1.ToString();

            Task.Run(() =>
            {
                LogFiles(butn1, 1);
            });                             
        }

        private void LogFiles(bool signal, int device)
        {
            bool sig = signal;
            int dev = device;

            Applicationss app = new Applicationss();
            app.SetData(signal, dev);
        }  
    }
}
namespace Interface
{
    class Driver
    {
        public void WriteResult(int device,int counter)
        {
            int unitnr = device;
            int cnt = counter;

            StreamWriter SW = new StreamWriter(@"logs.txt");

            if (SW.BaseStream != null)
            {
               
                Debug.WriteLine("File is written " + counter.ToString());
                SW.Write(unitnr.ToString(), +'-' + cnt.ToString());
                SW.Close();
            }
            else
            {
                Debug.WriteLine("Writer is busy");
            }
            
        }
    }
}

命名空间接口
{
   类应用程序
    {
       公共无效SetData(布尔信号,内部设备)
        {
            bool sig =信号;
            int dev =设备;
            int计数器= 0;

           同时(信号)
            {
               计数器=计数器++;
               驱动程序dr = new Interface.Driver();
                Dr.WriteResult(dev,counter);
                Debug.WriteLine(应用层" + counter.ToString());
            }    
        } 
    }
}

namespace Interface
{
    class Applicationss
    {
        public void SetData(bool signal,int device)
        {
            bool sig = signal;
            int dev = device;
            int counter = 0;

            while (signal)
            {
                counter = counter++;
                Driver dr = new Interface.Driver();
                dr.WriteResult(dev, counter);
                Debug.WriteLine("Application Layer" + counter.ToString());
            }     
        } 
    }
}

labjac

推荐答案

private Dictionary<int, Task> taskList = new Dictionary<int, Task>();

private void but1_Click(object sender, EventArgs e)
{           
    if (butn1)
    {
        butn1 = false;                
    }
    else
    {
        butn1 = true; 
    }
    txtd1.Text = butn1.ToString();

    Task taskLog = Task.Run(() =>
    {
        LogFiles(butn1, 1);
    }); 

    this.taskList.Add(1, taskLog);
}

希望对您有帮助...

Hope that helps ...


这篇关于多线程应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 16:09