-1

I inherited an old VB.NET program that was written long before I got to this job. I'm having to rewrite it in C#. I have been unable to find anything that seems to be a conversion for this. Can someone show me a translation for this in C#, please?

Private Sub Log(Message As String)

    Try

        If txtLog.InvokeRequired Then

            txtLog.Invoke(Sub()
                              Log(Message)
                          End Sub)
        Else
            txtLog.AppendText(Message & Environment.NewLine)
        End If

    Catch ex As Exception

    End Try

End Sub
  • Which part are you unsure about? The lambda/anonymous sub? – Konrad Rudolph Dec 22 '22 at 17:37
  • 4
    Does this answer your question? [Automating the InvokeRequired code pattern](https://stackoverflow.com/questions/2367718/automating-the-invokerequired-code-pattern) – NineBerry Dec 22 '22 at 17:43
  • Or use an [online code converter](https://converter.telerik.com/) – Olivier Jacot-Descombes Dec 22 '22 at 17:50
  • @OlivierJacot-Descombes every code converter gives an error on this. – Timothy Waters Dec 22 '22 at 18:21
  • [This one](https://converter.telerik.com/) worked for me. You have to click the button in the middle in order to have VB.NET to the left and C# to the right. Then copy paste your `Sub` into the left box. – Olivier Jacot-Descombes Dec 22 '22 at 18:36
  • All that stuff is just `txtLog.BeginInvoke(()=> txtLog.AppendText(Message + Environment.NewLine));` and nothing else. You also don't risk a deadlock. just an exception in case `txtLog` has been disposed in the meanwhile – Jimi Dec 23 '22 at 01:30

1 Answers1

-1

This is the code converted to C#:

private void Log(string Message)
{
    try
    {
        if (txtLog.InvokeRequired == true)
        {
            txtLog.Invoke(() =>
            {
                Log(Message);
            });
        }
        else
        {
            txtLog.AppendText(Message + Environment.NewLine);
        }
    }
    catch { }
}
Jonathan Barraone
  • 489
  • 1
  • 3
  • 20