<p>Events are synchronous in C#. I have this application where my main form starts a thread with a loop in it that listens to a stream. When something comes along on the stream an event is fired from the loop to the main form. </p>

<p>If the main form is slow or shows a messagebox or something the loop will be suspended. What is the best way around this? By using a callback and invoke on the main form?</p>

[{'answer_id': 80681, 'author': 'jfs', 'author_id': 718, 'author_profile': 'https://Stackoverflow.com/users/718', 'pm_score': 3, 'selected': False, 'text': '<p>Since you\'re using a form, the easier way is to use the <a href="https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.backgroundworker" rel="nofollow noreferrer">BackgroundWorker</a> component.</p>\n\n<blockquote>\n  <p>The BackgroundWorker class allows you\n  to run an operation on a separate,\n  dedicated thread. Time-consuming\n  operations like downloads and database\n  transactions can cause your user\n  interface (UI) to seem as though it\n  has stopped responding while they are\n  running. When you want a responsive UI\n  and you are faced with long delays\n  associated with such operations, the\n  BackgroundWorker class provides a\n  convenient solution.</p>\n</blockquote>\n'}, {'answer_id': 80686, 'author': 'Sam', 'author_id': 9406, 'author_profile': 'https://Stackoverflow.com/users/9406', 'pm_score': 3, 'selected': True, 'text': "<p>Hmmm, I've used different scenarios that depended on what I needed at the time.</p>\n\n<p>I believe the BeginInvoke would probably be the easiest to code since you're almost there. Either way you should be using Invoke already, so just changing to BeginInvoke. Using a callback on a separate thread will accomplish the same thing (as long as you use the threadpool to queue up the callback) as using BeginInvoke.</p>\n"}, {'answer_id': 80688, 'author': 'Davy Landman', 'author_id': 11098, 'author_profile': 'https://Stackoverflow.com/users/11098', 'pm_score': 1, 'selected': False, 'text': '<p>Events are just <a href="https://learn.microsoft.com/en-us/dotnet/api/system.eventhandler" rel="nofollow noreferrer">delegates</a>, so use BeginInvoke. (see <a href="https://learn.microsoft.com/en-us/archive/msdn-magazine/2001/august/net-delegates-making-asynchronous-method-calls-in-the-net-environment" rel="nofollow noreferrer">Making Asynchronous Method Calls in the .NET Environment</a>)</p>\n'}, {'answer_id': 80725, 'author': 'Raithlin', 'author_id': 6528, 'author_profile': 'https://Stackoverflow.com/users/6528', 'pm_score': 0, 'selected': False, 'text': "<p>You have a few options, as already detailed, but in my experience, you're better off leaving delegates and BeginInvoke, and using BackgroundWorker instead (v2.0+), as it is easier to use and also allows you to interact with the main form on the thread's completion. All in all a very weel implemented solution, I have found.</p>\n"}, {'answer_id': 80959, 'author': 'el2iot2', 'author_id': 8668, 'author_profile': 'https://Stackoverflow.com/users/8668', 'pm_score': 0, 'selected': False, 'text': "<p><strong>System.ComponentModel.BackgroundWorker</strong> is indeed a good starting point. It will do your asynchronous work, give you notifications of important events, and has ways to better integrate with your forms. </p>\n\n<p>For example, you can activate progress notifications by registering a handler for the ProgressChanged event. (which is highly recommended if you have a long, asynchronous process and you don't want your user to think the application froze)</p>\n"}]
['https://Stackoverflow.com/questions/80645', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9077/']