How can I fire an event without waiting for the event listeners to run?

安稳与你 提交于 2019-12-06 06:29:52

You could do an async invoke when raising the event (as mentioned), or just raise the event itself on a background thread:

void OnUpdated(EventArgs e) {
   EventHandler h = this.Updated;
   if (h != null) h(e);
}

void DoStuff() {
   BigMethod();
   ThreadPool.QueueUserWorkItem(OnUpdated, EventArgs.Empty);
   BigMethod2();
}

If you raise asynchronously, multiple listeners would be processing your event at the same time. At the very least, that requires a thread-safe EventArg class. If you expect them to interact with your class as well, then you should document very carefully or make it thread-safe as well.

Raising the event on a background thread carries the same caveats for your class methods, but you don't have to worry about the EventArgs class itself.

It sounds like you are trying to invoke the delegates in the event's invocation list asynchronously.

I would suggest that you read .NET Asynchronous Events To Send Process Status To User Interface:

The .NET Framework offers us the concept of raising events (and other items) in our classes asynchronously. This means that we can raise the event in such a way as to not have the subscriber to that event (typically the user interface) hold up the processing in the method that raised the event. The benefit being that it doesn't negatively impact the performance of our business layer method.

Have the first event do nothing but kick off the thread, then it doesn't matter what other event listeners there are.

For your case 2 of back up program. The code sample will fire a file copy asynchronously and once the copy is done it calls the call back method. In the callback if you dont want to wait for UI to update, then you will have to call the UI updating code asynchronously

You can use asynchronous delegates

public class AsyncFileCopier
    {
        public delegate void FileCopyDelegate(string sourceFile, string destFile);

        public static void AsynFileCopy(string sourceFile, string destFile)
        {
            FileCopyDelegate del = new FileCopyDelegate(FileCopy);
            IAsyncResult result = del.BeginInvoke(sourceFile, destFile, CallBackAfterFileCopied, null);
        }

        public static void FileCopy(string sourceFile, string destFile)
        { 
            // Code to copy the file
        }

        public static void CallBackAfterFileCopied(IAsyncResult result)
        {
            // Notify UI by calling an async del (probably using fire & forget approach or another callback if desired)
        }
    }

You can call it as:

AsyncFileCopier.AsynFileCopy("abc.txt", "xyz.txt");

This link tells you the different techniques of asyn coding

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!