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
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
Comment