views:

141

answers:

2

I would like to make a delegate available to an entire class. The point of this is to allow a called method from an external class' backgroundWorker to continually report back through all of it's methods (ExternalClass.Run(); calls ExternalClass.Method2(); ExternalClass.Method3(); etc and they all need to send several progress reports. It seems inefficient to have to continually pass the delegate.

I've tried initializing an instance of the delegate globally and setting it to equal the passed instance in Run(); for each method to then have available to it but I am given an error that a null object cannot be implicitly converted.

thanks!

I cannot show the code I am working with as I do not currently have it with me (it's on my laptop) but I will try to better explain now. PSEUDO-CODE:

class form1 : form {
    backgroundWorker_doWork()
    {
        Class2.Run();
    }

    backgroundWorker_OnProgressChange()
    {
         // do this
    }

}


class class2{
     Run(){
     OtherMethod();ThirdMethod();
     }

     OtherMethod(){ //need to call backgroundWorker.ReportProcess(int, string)}
     ThirdMethod(){ //need to call backgroundWorker.ReportProcess(int, string)}
}

I really don't want to have to pass it every time is the point, i'd like to somehow pass it to class2

+4  A: 

You should show your code that isn't working and the exact error message. It should be fine - here's an example:

using System;

class Demo
{
    private readonly Action action;

    public Demo(Action action)
    {
        this.action = action;
    }

    public void FirstMethod()
    {
        Console.WriteLine("In first method");
        action();
    }

    public void SecondMethod()
    {
        Console.WriteLine("In second method");
        action();
    }
}

class Test
{
    static void Main()
    {
        Demo demo = new Demo(() => Console.WriteLine("Action called"));

        demo.FirstMethod();
        demo.SecondMethod();
    }
}
Jon Skeet
Jon, how did you get an upvote for this? How many alts do you have :)
jim
@jim: Given a somewhat confusing description, I thought this would be a fairly simple demo of how to hold a delegate in a variable. Why do you think it's not helpful?
Jon Skeet
sorry Jon, only ribbing. :) No disrespect intended.
jim
A: 

You can use the InvokeMethod function from a backgroundWorker to allow the worker to execute any delegate, example below (also waits for the invoke to finish, which you may not need):

BackgroundWorker Function (C++.net)

BackgroundWorkerFunction()
{
::IAsyncResult ^ThreadResult;

SetTileCount_Delegate ^SetCountDel = gcnew SetTileCount_Delegate(this, &PartDetail::SetTileCount_Function);

//RecordingContainer is the class I am invoking into
ThreadResult = this->RecordingContainer->BeginInvoke(
   SetCountDel, ThisTest->RecordingsCache->Count);

WaitForInvokeTimeOutOrCompletion(ThreadResult);
}




System::Void WaitForInvokeTimeOutOrCompletion(IAsyncResult ^ThreadResult)
{
   if(ThreadResult == nullptr) return;

   long SleepTotal = 0;
   long SleepInterval = 100;

    while ((SleepTotal <= 2000) && !ThreadResult->IsCompleted) 
    {
       ThreadResult->AsyncWaitHandle->WaitOne(SleepInterval, false);
       SleepTotal += SleepInterval;
    }
}
greggorob64