Is this the proper way to inform the main thread of an event from a Background thread?Cross-thread operation not valid: Control accessed from a thread other than the thread it was created onIs there any way to kill a Thread?How do I update the GUI from another thread?Handle an event raised from background threadRunning code in main thread from another threadHow to check if current thread is not main threadJava : How do I call a method in a different class that extends Thread and is a part of a Thread Array from my main class?C# UDP Multicast with multiple clients - only receiving message from one clientC# .Net receiving UDp packets in separater thread and application exitCSharp async server program immediately closes at start

How do I extrude a face to a single vertex

Why did the HMS Bounty go back to a time when whales are already rare?

Should I stop contributing to retirement accounts?

Can someone explain how this makes sense electrically?

Drawing ramified coverings with tikz

How can Trident be so inexpensive? Will it orbit Triton or just do a (slow) flyby?

Create all possible words using a set or letters

What major Native American tribes were around Santa Fe during the late 1850s?

How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?

How will losing mobility of one hand affect my career as a programmer?

How do I implement a file system driver driver in Linux?

Will the technology I first learn determine the direction of my future career?

We have a love-hate relationship

Diode in opposite direction?

Could the E-bike drivetrain wear down till needing replacement after 400 km?

Is it possible to use .desktop files to open local pdf files on specific pages with a browser?

Can we have a perfect cadence in a minor key?

Does the Mind Blank spell prevent the target from being frightened?

Can a significant change in incentives void an employment contract?

What does this horizontal bar at the first measure mean?

List of people who lose a child in תנ"ך

Has Darkwing Duck ever met Scrooge McDuck?

Bob has never been a M before

Did arcade monitors have same pixel aspect ratio as TV sets?



Is this the proper way to inform the main thread of an event from a Background thread?


Cross-thread operation not valid: Control accessed from a thread other than the thread it was created onIs there any way to kill a Thread?How do I update the GUI from another thread?Handle an event raised from background threadRunning code in main thread from another threadHow to check if current thread is not main threadJava : How do I call a method in a different class that extends Thread and is a part of a Thread Array from my main class?C# UDP Multicast with multiple clients - only receiving message from one clientC# .Net receiving UDp packets in separater thread and application exitCSharp async server program immediately closes at start













0















The background-thread is an UDP listener. As soon as a certain type of message is received, the main thread has some work to do.
My current solution works. But I'm in doubt of the implementation.



My question:



  • Is this a proper way to handle these "simple" situations?

  • Is there another simple and elegant solution that's more commonly accepted?

The server class:



class UdpServer

UdpClient listener;

Messenger messenger;

public UdpServer(Messenger messenger)

this.messenger= messenger;


public void StartListening()

listener = new UdpClient(settings.Port);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 15000);

try

while(true)

byte[] bytes = listener.Receive(ref groupEP);

messenger.Message = string.Format("0 : 1",
groupEP.ToString(),
Encoding.ASCII.GetString(bytes, 0, bytes.Length));


catch(SocketException e)

messenger.Message = string.Format("UDP server error: 0", e.Message);

finally

listener.Close();





Thread "safety" is sort of implemented in the way that the thread reading the message will only check the value when the Event has been triggered. The event will only be triggered when the value is completely written. As long as each thread gets its own messenger instance, no trouble with threads sharing variables will arise, right?



The messenger class:



//this class is used to transport messages from the receiving threads to the main UI thread.
//subscribe to statusmessageevent in order to receive the messages

class Messenger

private string message;
public string Message

set

message = value;
StatusMessageEventHandler(message);



public event EventHandler<string> StatusMessageEvent;
private void StatusMessageEventHandler(string message)

StatusMessageEvent?.Invoke(this, message);




The main thread:



 static void Main(string[] args)


var UdpMessenger = new Messenger();

UdpMessenger.StatusMessageEvent += MessengerEvent;

var UdpServer = new UdpServer(UdpMessenger);

Task.Factory.StartNew(() => UdpServer.StartListening());

Console.ReadKey();



private static void MessengerEvent(object sender, string e)

Console.WriteLine(string.Format("Received Message: 0", e));










share|improve this question







New contributor




Micha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1





    If your code works and you're looking for design or performance improvement, a question on CodeReview might be more appropriate.

    – dymanoid
    Mar 21 at 13:50











  • Thank you, I had no idea of the separation between Stack Overflow and CodeReview

    – Micha
    Mar 21 at 13:51











  • Your design choice of using the Message property for an action/behavior is confusing and misleading. A property is more like a field or variable (with potential side-effects that can be implemented in the setter/getter) that in itself should not be used like a method that "does stuff". To make your code less confusing and easier to understand, you should use a method like SendMessage (or another meaningful name) when your code is trying to send a message. messenger.Message = ... looks like an ordinary assignment, not like some method call that does some action, wouldn't you agree?

    – elgonzo
    Mar 21 at 13:55











  • I agree, that would certainly make things more clear. Thank you.

    – Micha
    Mar 21 at 13:56












  • I was under the impression that because the instance was created on the main thread, the event would be called there too. At least it worked much better than when I tried to raise an event on the background thread. As that lead to very unpredictable behavior. However, this was exactly why I posted the question, I'm not sure what would be the proper way to implement this

    – Micha
    Mar 21 at 14:04
















0















The background-thread is an UDP listener. As soon as a certain type of message is received, the main thread has some work to do.
My current solution works. But I'm in doubt of the implementation.



My question:



  • Is this a proper way to handle these "simple" situations?

  • Is there another simple and elegant solution that's more commonly accepted?

The server class:



class UdpServer

UdpClient listener;

Messenger messenger;

public UdpServer(Messenger messenger)

this.messenger= messenger;


public void StartListening()

listener = new UdpClient(settings.Port);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 15000);

try

while(true)

byte[] bytes = listener.Receive(ref groupEP);

messenger.Message = string.Format("0 : 1",
groupEP.ToString(),
Encoding.ASCII.GetString(bytes, 0, bytes.Length));


catch(SocketException e)

messenger.Message = string.Format("UDP server error: 0", e.Message);

finally

listener.Close();





Thread "safety" is sort of implemented in the way that the thread reading the message will only check the value when the Event has been triggered. The event will only be triggered when the value is completely written. As long as each thread gets its own messenger instance, no trouble with threads sharing variables will arise, right?



The messenger class:



//this class is used to transport messages from the receiving threads to the main UI thread.
//subscribe to statusmessageevent in order to receive the messages

class Messenger

private string message;
public string Message

set

message = value;
StatusMessageEventHandler(message);



public event EventHandler<string> StatusMessageEvent;
private void StatusMessageEventHandler(string message)

StatusMessageEvent?.Invoke(this, message);




The main thread:



 static void Main(string[] args)


var UdpMessenger = new Messenger();

UdpMessenger.StatusMessageEvent += MessengerEvent;

var UdpServer = new UdpServer(UdpMessenger);

Task.Factory.StartNew(() => UdpServer.StartListening());

Console.ReadKey();



private static void MessengerEvent(object sender, string e)

Console.WriteLine(string.Format("Received Message: 0", e));










share|improve this question







New contributor




Micha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1





    If your code works and you're looking for design or performance improvement, a question on CodeReview might be more appropriate.

    – dymanoid
    Mar 21 at 13:50











  • Thank you, I had no idea of the separation between Stack Overflow and CodeReview

    – Micha
    Mar 21 at 13:51











  • Your design choice of using the Message property for an action/behavior is confusing and misleading. A property is more like a field or variable (with potential side-effects that can be implemented in the setter/getter) that in itself should not be used like a method that "does stuff". To make your code less confusing and easier to understand, you should use a method like SendMessage (or another meaningful name) when your code is trying to send a message. messenger.Message = ... looks like an ordinary assignment, not like some method call that does some action, wouldn't you agree?

    – elgonzo
    Mar 21 at 13:55











  • I agree, that would certainly make things more clear. Thank you.

    – Micha
    Mar 21 at 13:56












  • I was under the impression that because the instance was created on the main thread, the event would be called there too. At least it worked much better than when I tried to raise an event on the background thread. As that lead to very unpredictable behavior. However, this was exactly why I posted the question, I'm not sure what would be the proper way to implement this

    – Micha
    Mar 21 at 14:04














0












0








0








The background-thread is an UDP listener. As soon as a certain type of message is received, the main thread has some work to do.
My current solution works. But I'm in doubt of the implementation.



My question:



  • Is this a proper way to handle these "simple" situations?

  • Is there another simple and elegant solution that's more commonly accepted?

The server class:



class UdpServer

UdpClient listener;

Messenger messenger;

public UdpServer(Messenger messenger)

this.messenger= messenger;


public void StartListening()

listener = new UdpClient(settings.Port);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 15000);

try

while(true)

byte[] bytes = listener.Receive(ref groupEP);

messenger.Message = string.Format("0 : 1",
groupEP.ToString(),
Encoding.ASCII.GetString(bytes, 0, bytes.Length));


catch(SocketException e)

messenger.Message = string.Format("UDP server error: 0", e.Message);

finally

listener.Close();





Thread "safety" is sort of implemented in the way that the thread reading the message will only check the value when the Event has been triggered. The event will only be triggered when the value is completely written. As long as each thread gets its own messenger instance, no trouble with threads sharing variables will arise, right?



The messenger class:



//this class is used to transport messages from the receiving threads to the main UI thread.
//subscribe to statusmessageevent in order to receive the messages

class Messenger

private string message;
public string Message

set

message = value;
StatusMessageEventHandler(message);



public event EventHandler<string> StatusMessageEvent;
private void StatusMessageEventHandler(string message)

StatusMessageEvent?.Invoke(this, message);




The main thread:



 static void Main(string[] args)


var UdpMessenger = new Messenger();

UdpMessenger.StatusMessageEvent += MessengerEvent;

var UdpServer = new UdpServer(UdpMessenger);

Task.Factory.StartNew(() => UdpServer.StartListening());

Console.ReadKey();



private static void MessengerEvent(object sender, string e)

Console.WriteLine(string.Format("Received Message: 0", e));










share|improve this question







New contributor




Micha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












The background-thread is an UDP listener. As soon as a certain type of message is received, the main thread has some work to do.
My current solution works. But I'm in doubt of the implementation.



My question:



  • Is this a proper way to handle these "simple" situations?

  • Is there another simple and elegant solution that's more commonly accepted?

The server class:



class UdpServer

UdpClient listener;

Messenger messenger;

public UdpServer(Messenger messenger)

this.messenger= messenger;


public void StartListening()

listener = new UdpClient(settings.Port);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 15000);

try

while(true)

byte[] bytes = listener.Receive(ref groupEP);

messenger.Message = string.Format("0 : 1",
groupEP.ToString(),
Encoding.ASCII.GetString(bytes, 0, bytes.Length));


catch(SocketException e)

messenger.Message = string.Format("UDP server error: 0", e.Message);

finally

listener.Close();





Thread "safety" is sort of implemented in the way that the thread reading the message will only check the value when the Event has been triggered. The event will only be triggered when the value is completely written. As long as each thread gets its own messenger instance, no trouble with threads sharing variables will arise, right?



The messenger class:



//this class is used to transport messages from the receiving threads to the main UI thread.
//subscribe to statusmessageevent in order to receive the messages

class Messenger

private string message;
public string Message

set

message = value;
StatusMessageEventHandler(message);



public event EventHandler<string> StatusMessageEvent;
private void StatusMessageEventHandler(string message)

StatusMessageEvent?.Invoke(this, message);




The main thread:



 static void Main(string[] args)


var UdpMessenger = new Messenger();

UdpMessenger.StatusMessageEvent += MessengerEvent;

var UdpServer = new UdpServer(UdpMessenger);

Task.Factory.StartNew(() => UdpServer.StartListening());

Console.ReadKey();



private static void MessengerEvent(object sender, string e)

Console.WriteLine(string.Format("Received Message: 0", e));







c# multithreading






share|improve this question







New contributor




Micha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Micha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Micha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Mar 21 at 13:48









MichaMicha

83




83




New contributor




Micha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Micha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Micha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 1





    If your code works and you're looking for design or performance improvement, a question on CodeReview might be more appropriate.

    – dymanoid
    Mar 21 at 13:50











  • Thank you, I had no idea of the separation between Stack Overflow and CodeReview

    – Micha
    Mar 21 at 13:51











  • Your design choice of using the Message property for an action/behavior is confusing and misleading. A property is more like a field or variable (with potential side-effects that can be implemented in the setter/getter) that in itself should not be used like a method that "does stuff". To make your code less confusing and easier to understand, you should use a method like SendMessage (or another meaningful name) when your code is trying to send a message. messenger.Message = ... looks like an ordinary assignment, not like some method call that does some action, wouldn't you agree?

    – elgonzo
    Mar 21 at 13:55











  • I agree, that would certainly make things more clear. Thank you.

    – Micha
    Mar 21 at 13:56












  • I was under the impression that because the instance was created on the main thread, the event would be called there too. At least it worked much better than when I tried to raise an event on the background thread. As that lead to very unpredictable behavior. However, this was exactly why I posted the question, I'm not sure what would be the proper way to implement this

    – Micha
    Mar 21 at 14:04













  • 1





    If your code works and you're looking for design or performance improvement, a question on CodeReview might be more appropriate.

    – dymanoid
    Mar 21 at 13:50











  • Thank you, I had no idea of the separation between Stack Overflow and CodeReview

    – Micha
    Mar 21 at 13:51











  • Your design choice of using the Message property for an action/behavior is confusing and misleading. A property is more like a field or variable (with potential side-effects that can be implemented in the setter/getter) that in itself should not be used like a method that "does stuff". To make your code less confusing and easier to understand, you should use a method like SendMessage (or another meaningful name) when your code is trying to send a message. messenger.Message = ... looks like an ordinary assignment, not like some method call that does some action, wouldn't you agree?

    – elgonzo
    Mar 21 at 13:55











  • I agree, that would certainly make things more clear. Thank you.

    – Micha
    Mar 21 at 13:56












  • I was under the impression that because the instance was created on the main thread, the event would be called there too. At least it worked much better than when I tried to raise an event on the background thread. As that lead to very unpredictable behavior. However, this was exactly why I posted the question, I'm not sure what would be the proper way to implement this

    – Micha
    Mar 21 at 14:04








1




1





If your code works and you're looking for design or performance improvement, a question on CodeReview might be more appropriate.

– dymanoid
Mar 21 at 13:50





If your code works and you're looking for design or performance improvement, a question on CodeReview might be more appropriate.

– dymanoid
Mar 21 at 13:50













Thank you, I had no idea of the separation between Stack Overflow and CodeReview

– Micha
Mar 21 at 13:51





Thank you, I had no idea of the separation between Stack Overflow and CodeReview

– Micha
Mar 21 at 13:51













Your design choice of using the Message property for an action/behavior is confusing and misleading. A property is more like a field or variable (with potential side-effects that can be implemented in the setter/getter) that in itself should not be used like a method that "does stuff". To make your code less confusing and easier to understand, you should use a method like SendMessage (or another meaningful name) when your code is trying to send a message. messenger.Message = ... looks like an ordinary assignment, not like some method call that does some action, wouldn't you agree?

– elgonzo
Mar 21 at 13:55





Your design choice of using the Message property for an action/behavior is confusing and misleading. A property is more like a field or variable (with potential side-effects that can be implemented in the setter/getter) that in itself should not be used like a method that "does stuff". To make your code less confusing and easier to understand, you should use a method like SendMessage (or another meaningful name) when your code is trying to send a message. messenger.Message = ... looks like an ordinary assignment, not like some method call that does some action, wouldn't you agree?

– elgonzo
Mar 21 at 13:55













I agree, that would certainly make things more clear. Thank you.

– Micha
Mar 21 at 13:56






I agree, that would certainly make things more clear. Thank you.

– Micha
Mar 21 at 13:56














I was under the impression that because the instance was created on the main thread, the event would be called there too. At least it worked much better than when I tried to raise an event on the background thread. As that lead to very unpredictable behavior. However, this was exactly why I posted the question, I'm not sure what would be the proper way to implement this

– Micha
Mar 21 at 14:04






I was under the impression that because the instance was created on the main thread, the event would be called there too. At least it worked much better than when I tried to raise an event on the background thread. As that lead to very unpredictable behavior. However, this was exactly why I posted the question, I'm not sure what would be the proper way to implement this

– Micha
Mar 21 at 14:04













1 Answer
1






active

oldest

votes


















1














You write in the comments:




I was under the impression that because the instance was created on the main thread, the event would be called there too.




Objects in C# and .NET are not thread-affine by default. You need to implement this behavior manually. Creating a generic object on a thread doesn't cause that object to raise events on the thread the object was created on. Events are raised on the caller thread, if you don't provide a custom implementation that changes this.



With your current code, the StatusMessageEvent events will be raised on the caller thread (the one that runs the StartListening method).



If you have a GUI application (e.g. WPF, WinForms), you can manually marshal to the main thread when necessary.



class Program

static SynchronizationContext mainThreadContext;

static void Main()

// app initialization code here

// you could also do this somewhere in your main window
mainThreadContext = SynchronizationContext.Current;


static void MessengerEvent(object sender, EventArgs<string> e)

// do additional stuff here that can be done on a background thread

// The UpdateUI method will be executed on the UI thread
mainThreadContext.Post(_ => UpdateUI(), null);


static void UpdateUI()



Instead of the SynchronizationContext, you can use Control.BeginInvoke (WinForms) or Dispatcher.BeginInvoke (WPF).



If you have a console app and for some reason need to marshal to the main thread... Well, then you need to implement your own SynchronizationContext and something like a task scheduler or a dispatcher or a main loop. That's not easy.






share|improve this answer

























  • Thank you very much for your elaborate answer. I just realized I didn't explain one of my main concerns: When firing the event from another thread, is there any risk of the event getting lost?

    – Micha
    Mar 21 at 15:18












  • @Micha, there is always a risk that an event will be lost, e.g. when your app crashes or when some buffer overflows etc. Consider asking a different question about your case.

    – dymanoid
    Mar 21 at 16:07










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






Micha is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55281934%2fis-this-the-proper-way-to-inform-the-main-thread-of-an-event-from-a-background-t%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You write in the comments:




I was under the impression that because the instance was created on the main thread, the event would be called there too.




Objects in C# and .NET are not thread-affine by default. You need to implement this behavior manually. Creating a generic object on a thread doesn't cause that object to raise events on the thread the object was created on. Events are raised on the caller thread, if you don't provide a custom implementation that changes this.



With your current code, the StatusMessageEvent events will be raised on the caller thread (the one that runs the StartListening method).



If you have a GUI application (e.g. WPF, WinForms), you can manually marshal to the main thread when necessary.



class Program

static SynchronizationContext mainThreadContext;

static void Main()

// app initialization code here

// you could also do this somewhere in your main window
mainThreadContext = SynchronizationContext.Current;


static void MessengerEvent(object sender, EventArgs<string> e)

// do additional stuff here that can be done on a background thread

// The UpdateUI method will be executed on the UI thread
mainThreadContext.Post(_ => UpdateUI(), null);


static void UpdateUI()



Instead of the SynchronizationContext, you can use Control.BeginInvoke (WinForms) or Dispatcher.BeginInvoke (WPF).



If you have a console app and for some reason need to marshal to the main thread... Well, then you need to implement your own SynchronizationContext and something like a task scheduler or a dispatcher or a main loop. That's not easy.






share|improve this answer

























  • Thank you very much for your elaborate answer. I just realized I didn't explain one of my main concerns: When firing the event from another thread, is there any risk of the event getting lost?

    – Micha
    Mar 21 at 15:18












  • @Micha, there is always a risk that an event will be lost, e.g. when your app crashes or when some buffer overflows etc. Consider asking a different question about your case.

    – dymanoid
    Mar 21 at 16:07















1














You write in the comments:




I was under the impression that because the instance was created on the main thread, the event would be called there too.




Objects in C# and .NET are not thread-affine by default. You need to implement this behavior manually. Creating a generic object on a thread doesn't cause that object to raise events on the thread the object was created on. Events are raised on the caller thread, if you don't provide a custom implementation that changes this.



With your current code, the StatusMessageEvent events will be raised on the caller thread (the one that runs the StartListening method).



If you have a GUI application (e.g. WPF, WinForms), you can manually marshal to the main thread when necessary.



class Program

static SynchronizationContext mainThreadContext;

static void Main()

// app initialization code here

// you could also do this somewhere in your main window
mainThreadContext = SynchronizationContext.Current;


static void MessengerEvent(object sender, EventArgs<string> e)

// do additional stuff here that can be done on a background thread

// The UpdateUI method will be executed on the UI thread
mainThreadContext.Post(_ => UpdateUI(), null);


static void UpdateUI()



Instead of the SynchronizationContext, you can use Control.BeginInvoke (WinForms) or Dispatcher.BeginInvoke (WPF).



If you have a console app and for some reason need to marshal to the main thread... Well, then you need to implement your own SynchronizationContext and something like a task scheduler or a dispatcher or a main loop. That's not easy.






share|improve this answer

























  • Thank you very much for your elaborate answer. I just realized I didn't explain one of my main concerns: When firing the event from another thread, is there any risk of the event getting lost?

    – Micha
    Mar 21 at 15:18












  • @Micha, there is always a risk that an event will be lost, e.g. when your app crashes or when some buffer overflows etc. Consider asking a different question about your case.

    – dymanoid
    Mar 21 at 16:07













1












1








1







You write in the comments:




I was under the impression that because the instance was created on the main thread, the event would be called there too.




Objects in C# and .NET are not thread-affine by default. You need to implement this behavior manually. Creating a generic object on a thread doesn't cause that object to raise events on the thread the object was created on. Events are raised on the caller thread, if you don't provide a custom implementation that changes this.



With your current code, the StatusMessageEvent events will be raised on the caller thread (the one that runs the StartListening method).



If you have a GUI application (e.g. WPF, WinForms), you can manually marshal to the main thread when necessary.



class Program

static SynchronizationContext mainThreadContext;

static void Main()

// app initialization code here

// you could also do this somewhere in your main window
mainThreadContext = SynchronizationContext.Current;


static void MessengerEvent(object sender, EventArgs<string> e)

// do additional stuff here that can be done on a background thread

// The UpdateUI method will be executed on the UI thread
mainThreadContext.Post(_ => UpdateUI(), null);


static void UpdateUI()



Instead of the SynchronizationContext, you can use Control.BeginInvoke (WinForms) or Dispatcher.BeginInvoke (WPF).



If you have a console app and for some reason need to marshal to the main thread... Well, then you need to implement your own SynchronizationContext and something like a task scheduler or a dispatcher or a main loop. That's not easy.






share|improve this answer















You write in the comments:




I was under the impression that because the instance was created on the main thread, the event would be called there too.




Objects in C# and .NET are not thread-affine by default. You need to implement this behavior manually. Creating a generic object on a thread doesn't cause that object to raise events on the thread the object was created on. Events are raised on the caller thread, if you don't provide a custom implementation that changes this.



With your current code, the StatusMessageEvent events will be raised on the caller thread (the one that runs the StartListening method).



If you have a GUI application (e.g. WPF, WinForms), you can manually marshal to the main thread when necessary.



class Program

static SynchronizationContext mainThreadContext;

static void Main()

// app initialization code here

// you could also do this somewhere in your main window
mainThreadContext = SynchronizationContext.Current;


static void MessengerEvent(object sender, EventArgs<string> e)

// do additional stuff here that can be done on a background thread

// The UpdateUI method will be executed on the UI thread
mainThreadContext.Post(_ => UpdateUI(), null);


static void UpdateUI()



Instead of the SynchronizationContext, you can use Control.BeginInvoke (WinForms) or Dispatcher.BeginInvoke (WPF).



If you have a console app and for some reason need to marshal to the main thread... Well, then you need to implement your own SynchronizationContext and something like a task scheduler or a dispatcher or a main loop. That's not easy.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 21 at 15:21

























answered Mar 21 at 15:06









dymanoiddymanoid

9,68822349




9,68822349












  • Thank you very much for your elaborate answer. I just realized I didn't explain one of my main concerns: When firing the event from another thread, is there any risk of the event getting lost?

    – Micha
    Mar 21 at 15:18












  • @Micha, there is always a risk that an event will be lost, e.g. when your app crashes or when some buffer overflows etc. Consider asking a different question about your case.

    – dymanoid
    Mar 21 at 16:07

















  • Thank you very much for your elaborate answer. I just realized I didn't explain one of my main concerns: When firing the event from another thread, is there any risk of the event getting lost?

    – Micha
    Mar 21 at 15:18












  • @Micha, there is always a risk that an event will be lost, e.g. when your app crashes or when some buffer overflows etc. Consider asking a different question about your case.

    – dymanoid
    Mar 21 at 16:07
















Thank you very much for your elaborate answer. I just realized I didn't explain one of my main concerns: When firing the event from another thread, is there any risk of the event getting lost?

– Micha
Mar 21 at 15:18






Thank you very much for your elaborate answer. I just realized I didn't explain one of my main concerns: When firing the event from another thread, is there any risk of the event getting lost?

– Micha
Mar 21 at 15:18














@Micha, there is always a risk that an event will be lost, e.g. when your app crashes or when some buffer overflows etc. Consider asking a different question about your case.

– dymanoid
Mar 21 at 16:07





@Micha, there is always a risk that an event will be lost, e.g. when your app crashes or when some buffer overflows etc. Consider asking a different question about your case.

– dymanoid
Mar 21 at 16:07












Micha is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















Micha is a new contributor. Be nice, and check out our Code of Conduct.












Micha is a new contributor. Be nice, and check out our Code of Conduct.











Micha is a new contributor. Be nice, and check out our Code of Conduct.














Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55281934%2fis-this-the-proper-way-to-inform-the-main-thread-of-an-event-from-a-background-t%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript