Announcement

Collapse
No announcement yet.

invoke() in statischer Methode

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • invoke() in statischer Methode

    Hallo Experten,

    ich möchte etwas in eine TextBox schreiben. Das Schreiben selbst darf nur von dem Thread aus geschehen, der die TexBox erstellt hat, also der UI-Thread.
    Um von anderen Programmteilen aus schreiben zu können, habe ich folgende Funktion erstellt:[highlight=c#] private delegate void LogToScreenCallback(System.Windows.Forms.TextBox tclLog, string strNewLogEntry);

    /// <summary>
    /// Adds a log message preceded by logging time to the given TextBox control
    /// </summary>
    /// <param name="tclLog">The TextBox control to log to</param>
    /// <param name="strNewLogEntry">The message to log</param>
    public static void LogToScreen(System.Windows.Forms.TextBox tclLog, string strNewLogEntry)
    {
    if (tclLog == null) return;
    if (tclLog.InvokeRequired)
    {
    LogToScreenCallback callback = new LogToScreenCallback(LogToScreen);
    tclLog.invoke(callback, new object[] { tclLog, strNewLogEntry });
    }
    else
    {
    tclLog.SuspendLayout();
    tclLog.AppendText("\r\n[" + System.DateTime.Now.ToString("T") + "] " + strNewLogEntry);
    if (tclLog.Text.Length > tclLog.MaxLength)
    tclLog.Text = tclLog.Text.Substring(tclLog.MaxLength / 2);

    tclLog.ResumeLayout();
    }
    }[/highlight]Solange alles in einem Thread lief, war das auch kein Problem. Jetzt soll es mehrere Threads geben. Deshalb habe ich die Verzweigung if (tclLog.InvokeRequired) eingefügt.
    Allerdings ist für tclLog kein invoke() definiert.
    Normalerweise nähme ich this.invoke(). Das verbietet sich aber hier, weil die Funktion static ist.

    Kann ich herausfinden, zu welcher Form die übergebene TextBox gehört?
    tclLog.Parent.invoke() habe ich schon versucht, für Control ist invoke aber ebensowenig definiert.

    Mit freundlichem Gruß


    luker

  • #2
    Hallo,

    kurze Zwischenfragen:
    • Muss die Methode "static" sein?
    • Kann stattdessen ein Backgroundworker verwendet werden?


    mfG Gü
    "Any fool can write code that a computer can understand. Good programmers write code that humans can understand". - Martin Fowler

    Comment


    • #3
      Hat sich erledigt. TextBox.Invoke() gehört großgeschrieben.


      Aber zu Deinen Fragen:

      Ja, die Methode muss static sein.

      Wo genau würdest Du einen Backgroundworker benutzen?

      Comment


      • #4
        Originally posted by luker View Post
        Wo genau würdest Du einen Backgroundworker benutzen?
        Verwendung um aus einem anderen Thread den UI-Thread zu aktualisieren. Der Backroundworker kann hier hilfreich sein da man sich nicht um das Invoke kümmern müss -> mehr Infos unter der MSDN.

        mfG Gü
        "Any fool can write code that a computer can understand. Good programmers write code that humans can understand". - Martin Fowler

        Comment

        Working...
        X