Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to design multithreaded .NET application with processes that listen and send data?
    primarykey
    data
    text
    <p>I am trying to design a multithreaded .NET application that can execute a number of processes simultaneously. I have no experience with multithreaded applications and have spent the last few days reading up on it. Here is what I am needing to do. I will have two processes that "listen" for UDP broadcast data on some interval. Then I will have a third process that sends out the most recent data on some interval. Right now I am building a proof of concept application. I am building it as a C# WinForms application. My idea is to have a class instance that I will use to hold the most recent data and I will use locking to make sure I don't have any issues.</p> <p>My application sort of works. It seems to run for about 60 seconds or so and then it just sort of stops working. It doesn't really fail with an error or anything. Sometimes it seems like it just stop sending. Other times the application will just close without any errors. Makes me think that I am having multithreading synchronization problems or something.</p> <p>Here is some simplified code:</p> <pre><code>public class MyData { public string Field1 {get; set;} public string Field2 {get; set;} } public class myForm : Form { private MyData _data = new MyData(); readonly object _locker = new object(); Thread _thread1; Thread _thread2; Thread _thread3; public myForm() { _thread1 = new Thread(new ThreadStart(Listener1)); _thread2 = new Thread(new ThreadStart(Listener2)); _thread3 = new Thread(new ThreadStart(Sender)); _thread1.Start(); _thread2.Start(); _thread3.Start(); } private void Listener1() { bool _done = false; while (!_done) { ...listen and get data... lock(_locker) { _data.Field1 = ...value... } } } private void Listener2() { bool _done = false; while (!_done) { ...listen and get data... lock(_locker) { _data.Field2 = ...value... } } } private void Sender() { string _dataToSend = ""; bool _done = false; while(!_done) { Thread.Sleep(2000); lock(_locker) { _dataToSend = _data.Field1 + "," + _data.Field2; } } ...send _dataToSend... } } </code></pre> <p>Does anyone have any ideas about what I may be doing wrong? Does this concept seem correct? Are there better ways that I could accomplish this same thing? Instead of using a class variable to hold the data (MyData), would it be better if I just read/write a file on the file system from the different threads?</p> <p>Thanks, Corey</p> <p>EDITED: I left out the code to start the threads.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload