I'm making a class library so I can show reports, graphs and other things for an ADC. I'm making this as a class library so I can just include a dll in any projects which will need it.
Currently, when I run a function from it the program will seem like it is unresponsive, this is because the only thread (main application thread) is being entirely used for the class library function that has been called.
The way to fix this would be to use a background worker which invokes a function and then runs that invoked function - Is my thinking correct here?
From that basis I have started making my own class (Though, I've never used background workers before and fairly new to C#, I use C++)
The whole basis around my backgroundWorker
class is ease of use. I want to have a simple function where I can put in a method, and then it will run that method in another thread. Kind of trying to go for simplicity here.
The code I have at the moment;
class backgroundWorker
{
public BackgroundWorker bw;
public delegate void RunFunction();
public RunFunction thisFunction;
public backgroundWorker(RunFunction newFunction)
{
thisFunction = newFunction;
bw = new BackgroundWorker();
bw.WorkerReportsProgress = true;
bw.DoWork += bw_DoWork;
bw.ProgressChanged += bw_ProgressChanged;
bw.RunWorkerCompleted += bw_RunWorkerCompleted;
}
}
public void start()
{
bw.RunWorkerAsync(thisFunction);
}
This is the first snip of the class. When I create the default constructor, in the parenthesis I put in the function that I want to use;
if (tabControl1.SelectedIndex == 1)
{
newNiDevice.setInputs(newNiDevice.Ai1, newNiDevice.Ai2, newNiDevice.Ai3, newNiDevice.Ai4,);
newNiDevice.frequency = 1000; // 1000 samples per second
newNiDevice.time = 10; // 10 seconds
bgW = new backgroundWorker(newNiDevice.createMultiSample);
bgW.start();
// newNiDevice.createMultiSample();
newNiDevice.populateChartMulti(chart1);
}
The above code it used to sample a 10V power supply that gives a voltage, my program then takes the data, reads from certain channels with a frequency (Hz - samples per second) and the amount of time. It then puts it in a List and then plots it to a line chart. - Don't need to worry about this part, code works perfectly.
So, I was thinking wouldn't it just require me to create the default constructor where I call the newNiDevice.createMultiSample();
function? I hope I can get to that point, but at the moment it just crashes. That isn't what my question is about.
Which is present after the attempted creation of the multisample to plot to a chart - my thinking, how can it plot a chart without any data? So, the function call isn't being started.