I have a little MVC Pattern application that creates UDP packets with random data, and constantly sends it
The main view contains the controller:
public partial class MainForm : Form
{
private MainController controller;
public MainForm(MainController c)
{
controller = c;
InitializeComponent();
}
//...
}
The main button click event calls the method that will eventually start the emulation. I wrap it around a try-cath block so I can display any exception on the view
public partial class MainForm : Form
{
//...
private void btnInitiate_Click(object sender, EventArgs e)
{
try
{
controller.initiateEmulation(txtData.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + ex.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
public class MainController:IMainController
{
private Emulator model;
public void initiateEmulation(string data)
{
model = new Emulator(data);
}
}
public class Emulator
{
private Thread emulatorThread;
public String data;
public Emulator(string data)
{
this.data = data;
emulatorThread = new Thread(Emulate);
emulatorThread.Start();
}
private void Emulate()
{
//CREATES SOCKET
while (true)
{
//SENDS RANDOMIZED DATA
}
}
}
Problem is, my try-catch block is only capturing exceptions occurring in the main thread
How can I handle exceptions inside emulatorThread so I can show them on the view, the same as in the main thread?