Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 代表,开始行动。EndInvoke-如何清除对同一委托的多个异步威胁调用?_Vb.net_Delegates_Multithreading_Begininvoke - Fatal编程技术网

Vb.net 代表,开始行动。EndInvoke-如何清除对同一委托的多个异步威胁调用?

Vb.net 代表,开始行动。EndInvoke-如何清除对同一委托的多个异步威胁调用?,vb.net,delegates,multithreading,begininvoke,Vb.net,Delegates,Multithreading,Begininvoke,我已经创建了一个要调用Async的委托 模块级 下面是我在方法中使用的代码 该方法运行并执行它需要的操作 Asyn回调在我执行EndInvoke的完成时启动 只要在委托上启动BeginInvoke的方法仅在尚未运行BeginInvoke/线程操作的情况下运行,它就可以工作。问题是,当委托上的另一个线程仍在运行且尚未结束调用时,可以调用新线程 如果需要,程序需要能够让委托一次在多个实例中运行,并且它们都需要完成并调用EndInvoke。一旦启动另一个BeginInvoke,我就会丢失对第一个Beg

我已经创建了一个要调用Async的委托

模块级 下面是我在方法中使用的代码 该方法运行并执行它需要的操作

Asyn回调在我执行EndInvoke的完成时启动 只要在委托上启动BeginInvoke的方法仅在尚未运行BeginInvoke/线程操作的情况下运行,它就可以工作。问题是,当委托上的另一个线程仍在运行且尚未结束调用时,可以调用新线程

如果需要,程序需要能够让委托一次在多个实例中运行,并且它们都需要完成并调用EndInvoke。一旦启动另一个BeginInvoke,我就会丢失对第一个BeginInvoke的引用,因此无法使用EndInvoke清理新线程


克服此问题的干净解决方案和最佳实践是什么?

您只需保留对学员的一次引用;您不需要每次调用时都创建一个新的

不要将
Nothing
传递到
EndInvoke
,而是传递
ar
。这将为您提供该特定调用的结果

Sub AsyncCallback_GetPartListDataFromServer(ByVal ar As IAsyncResult)
    dlgGetPartList.EndInvoke(ar)
End Sub

如果您希望能够取消特定调用,那么您需要保留
BeginInvoke
的结果(这是在上面的回调中传递给您的
IAsyncResult
的同一个实例)。

调用BeginInvoke时,需要将对象作为状态参数传递

    class Program

    {
        delegate void SampleDelegate(string message);

        static void SampleDelegateMethod(string message)
        {
            Console.WriteLine(message);
        }
        static void Callback(object obj)
        {
            IAsyncResult result = obj as IAsyncResult;

            SampleDelegate del = result.AsyncState as SampleDelegate; 
            del.EndInvoke(result);
            Console.WriteLine("Finished calling EndInvoke");
        }
        static void Main()
        {
            for (int i = 0; i < 10; i++)
            {
                // Instantiate delegate with named method:
                SampleDelegate d1 = SampleDelegateMethod;
               //d1 is passed as a state
                d1.BeginInvoke("Hello", Callback, d1);
            }
            Console.WriteLine("Press any key to continue");
            Console.ReadLine();
        }
    }
类程序
{
delegate void SampleDelegate(字符串消息);
静态void SampleDelegateMethod(字符串消息)
{
控制台写入线(消息);
}
静态无效回调(对象obj)
{
IAsyncResult结果=对象作为IAsyncResult;
SampleDelegate del=result.AsyncState作为SampleDelegate;
del.EndInvoke(结果);
WriteLine(“已完成调用EndInvoke”);
}
静态void Main()
{
对于(int i=0;i<10;i++)
{
//使用命名方法实例化委托:
SampleDelegate d1=SampleDelegateMethod;
//d1作为一个州通过
d1.开始通话(“你好”,回拨,d1);
}
Console.WriteLine(“按任意键继续”);
Console.ReadLine();
}
}
Sub AsyncCallback_GetPartListDataFromServer(ByVal ar As IAsyncResult)
    dlgGetPartList.EndInvoke(Nothing)
End Sub
Sub AsyncCallback_GetPartListDataFromServer(ByVal ar As IAsyncResult)
    dlgGetPartList.EndInvoke(ar)
End Sub
    class Program

    {
        delegate void SampleDelegate(string message);

        static void SampleDelegateMethod(string message)
        {
            Console.WriteLine(message);
        }
        static void Callback(object obj)
        {
            IAsyncResult result = obj as IAsyncResult;

            SampleDelegate del = result.AsyncState as SampleDelegate; 
            del.EndInvoke(result);
            Console.WriteLine("Finished calling EndInvoke");
        }
        static void Main()
        {
            for (int i = 0; i < 10; i++)
            {
                // Instantiate delegate with named method:
                SampleDelegate d1 = SampleDelegateMethod;
               //d1 is passed as a state
                d1.BeginInvoke("Hello", Callback, d1);
            }
            Console.WriteLine("Press any key to continue");
            Console.ReadLine();
        }
    }