-1

any help much appreciated!

I have a UDP listener which has button controls to start/stop the UDP, and write to .txt file, however the latter part does not work as I wish.

The beginInvoke sends the 'data' to thread [button_start_writing_Click] causing it to trigger itself, rather than when I click the button.

How would I like it so the data is continually received in the textbox, then I choose the moment to start logging to txt file?

Many thanks in advance!

public partial class Form1 : Form
{

    private UdpClient Client;

    public Form1()
    {
        InitializeComponent();
    }

    private void button_start_Click(object sender, EventArgs e)
    {
        Client = new UdpClient(Convert.ToInt32(textBox_port.Text));
        Client.BeginReceive(DataReceived, null);

        button_stop.Click += Button_stop_Click;
    }

    private void DataReceived(IAsyncResult ar)
    {
        IPEndPoint ip = new IPEndPoint(IPAddress.Any, Convert.ToInt32(textBox_port.Text));

        byte[] data;
        try
        {
            data = Client.EndReceive(ar, ref ip);

            if (data.Length == 0)
                return; // No more to receive
            Client.BeginReceive(DataReceived, null);
        }
        catch //(ObjectDisposedException)
        {
            return; // Connection closed
        }

        // Send the data to the UI thread
        this.BeginInvoke((Action<IPEndPoint, string>)DataReceivedUI, ip, Encoding.UTF8.GetString(data));

        // Send the data to the writer thread
        this.BeginInvoke((Action<IPEndPoint, string>)button_start_writing_Click, ip, Encoding.UTF8.GetString(data));

    private void button_start_writing_Click(object sender, EventArgs e)
    {
        string logfilePath = "C:\\Users\\I4T\\Desktop\\Test Output\\";
        File.AppendAllText(logfilePath + "logtest.txt", "[" + DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss.fff") + "] " + data);
    }
Jon
  • 21
  • 1
  • 4
  • This isnt a complete minimal set of code, you should be able to trace to see why its not working. you havent defined what is wrong.. your "start writing" just dumps "data" which hasnt been defined so this code clearly doesnt compile.. – BugFinder Oct 02 '17 at 10:25
  • Sorry, I just noticed a typo from testing something else....DoSomething now replaced with button_start_writing_Click in the BeginInvoke. Perhaps the way I am approaching it is not the best. I am dumping the data (using beginInvoke) into the button_start_writing_Click thread. But in doing so, unfortunately the beginInvoke from the top thread is triggering the write to txt file thread, instead of me clicking the 'Start Writing' button. Any suggestions for a better method to achieve this? – Jon Oct 02 '17 at 10:51
  • because you told it to - so far you have nothing more than a simple logic / design issue – BugFinder Oct 02 '17 at 10:56
  • Definitely agree! I wondered if I should use a backgroundworker? But I am not sure if that is a better approach or how to use that for button clicks. Or maybe nested threads? – Jon Oct 02 '17 at 11:37
  • I've spent hours trying a few different things but this has really got me! I'm from a Matlab background, so I can't help thinking with a Matlab mindset, and I don't think that is helping. – Jon Oct 02 '17 at 11:40

1 Answers1

0

Ok - this is going to be too long for a comment but will not be giving you complete code cos Im mean!

Currently your code says

make connection
while data arrives
   write to text box
   write to file
end

which kinda is right but not right all at the same time - partly because its always going to call the dump to file..

what your code should do is

make connection
while data arrived
   write to text box
   if file logging is wanted write this data to end of file
end

button- toggle the logging.

So - how can you do that? Well there are logs of things you could do and some probably deemed more "normal" than others. Sticking to your fundamental question and not how to turn this code to glory you have noticed the if in my second set of code.

At the top of the class all this belongs to - some form, add a boolean FileLogging, then on button press change it to !FileLogging.

Your actions shouldnt be sender,eventargs but string so you can send the data to them, then you should be able to toggle the writing to file on and off (optionally adding Logging started/ended at..)as well as controlling when it actually did the logging.

Then you should find more of what you wanted

BugFinder
  • 17,474
  • 4
  • 36
  • 51