using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Threading; using System.Web; using lj; using LabJack.LabJackUD; namespace FormTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Thread t1 = new Thread(new ThreadStart(DetectRun)); t1.Start(); } void DetectRun() { while(true) { int[] idList = GetAllLabJacks(); // Check to see if any LabJacks were found if (idList.Length != 0) { MessageBox.Show("LabJack pluged in!"); break; } Thread.Sleep(3000); } } public static int[] GetAllLabJacks() { // Make sure we allocate space for what is passed int[] productIDList = new int[127]; int[] serialNumList = new int[127]; int[] localIDList = new int[127]; int[] powerList = new int[127]; int[,] calMatrix = new int[127, 20]; int numFound = 0; int reserved1 = 0, reserved2 = 0; // Call the ListAll function. We must use the keyword ref for parameters // that aren't arrays that return data int result = LabJack.ListAll(productIDList, serialNumList, localIDList, powerList, calMatrix, ref numFound, ref reserved1, ref reserved2); if (result != 0) ThrowErrorMessage("Unable to enumerate controllers", result); int[] ljs = new int[numFound]; int i = 0; // count how many we found and set // the array which will be returned // to contain valid IDs foreach (int id in localIDList) { if (id != 9999) { ljs[i] = id; ++i; } } // return that array return ljs; } } }