1

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.

Johnathan Brown
  • 693
  • 10
  • 30
  • 1
    What leads you to the assumption that the `NullReferenceException` has anything to do with the `BackgroundWorker`? I guess you use an uninitialized variable somewhere, and you have to find out in which line the exception is raised, by debugging!? – JohnB Jul 29 '14 at 09:30
  • See full [stack trace](http://msdn.microsoft.com/en-us/library/system.exception.stacktrace(v=vs.110).aspx) of the exception - VS usually stops whenever an unhandled exception occurs to allow you to debug it. It will help you to undestand whether the exception has something to do with BackgroundWorker or not. – Eugene Podskal Jul 29 '14 at 09:33
  • Almost all the cases `NullReferenceException` means the same thing. Refer to the suggested duplicate, use debugger to inspect what is null. – Sriram Sakthivel Jul 29 '14 at 09:33
  • The thing is I know it isn't grabbing any data because it is meant to be running for 10 seconds, it does it instantly. – Johnathan Brown Jul 29 '14 at 09:43
  • I know what a null reference is and how to fix it. Like I said, the part of my code which runs the multiSample works just fine, the part that isn't working is trying to get it to run in another thread. Once that part is working and the List gets the data it needs to, I won't get that error. – Johnathan Brown Jul 29 '14 at 09:52

0 Answers0