C# 使委托可供类使用

发布于 2024-08-02 06:35:02 字数 786 浏览 5 评论 0原文

我想为全班派一名代表。这样做的目的是允许从外部类的backgroundWorker调用的方法不断地报告它的所有方法(ExternalClass.Run();调用ExternalClass.Method2();ExternalClass.Method3();等等,它们都需要发送多个进度报告似乎效率很低,

我已经尝试全局初始化委托实例并将其设置为等于 Run() 中传递的实例,然后才能使用它。但我收到一个错误,无法隐式转换空对象,

谢谢!

我无法显示我正在使用的代码,因为我目前没有它(它在我的笔记本电脑上),但我现在会尝试更好地解释。伪代码:

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)}
} 

我真的不想每次都通过它,这是重点,我想以某种方式将它传递给 class2

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看透却不说透 2024-08-09 06:35:02

您应该显示无法运行的代码以及确切的错误消息。应该没问题 - 这是一个例子:

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();
    }
}

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();
    }
}
缘字诀 2024-08-09 06:35:02

您可以使用 backgroundWorker 中的 InvokeMethod 函数来允许工作线程执行任何委托,如下所示(还等待调用完成,您可能不需要):

BackgroundWorker 函数 (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;
    }
}

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文