Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't raise events in separated threads #9

Open
ALiwoto opened this issue Jun 23, 2021 · 0 comments
Open

Don't raise events in separated threads #9

ALiwoto opened this issue Jun 23, 2021 · 0 comments
Assignees
Labels
Big changes Some big changes have been applied enhancement New feature or request GraphicElement this is related to graphic elements

Comments

@ALiwoto
Copy link
Owner

ALiwoto commented Jun 23, 2021

Using Task.Run is not acceptable everywhere

	[EditorBrowsable(EditorBrowsableState.Never)]
	protected internal virtual void OnRightUp()
	{
		Task.Run((() =>
		{
			// raise the event in another thread.
			this.RightUp?.Invoke(this, null);
		}));
	}

So what's wrong with this code?

  1. You are creating a new thread, even if the action won't do anything.
  2. You are running every single event on a whole separated thread.

This will cause some problems in the future, such as hug memory leak.


So what to do?

I think something like this will be okay:

	[EditorBrowsable(EditorBrowsableState.Never)]
	protected internal virtual void OnRightUp()
	{
		this.RightUp?.Invoke(this, null);
		if (this.RightUpAsync != null)
		{
			Task.Run((() =>
			{
				// raise the event in another thread.
				this.RightUpAsync?.Invoke(this, null);
			}));
		}
	}

This way, we won't create new threads over and over.

@ALiwoto ALiwoto added Big changes Some big changes have been applied enhancement New feature or request GraphicElement this is related to graphic elements labels Jun 23, 2021
@ALiwoto ALiwoto added this to the Graphic Elements milestone Jun 23, 2021
@ALiwoto ALiwoto pinned this issue Jun 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Big changes Some big changes have been applied enhancement New feature or request GraphicElement this is related to graphic elements
Projects
None yet
Development

No branches or pull requests

3 participants