dotnet-snippets.de
Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1405 | Anzahl registrierter User: 1436 | Besucher online: 4
Hauptmenü
Home
Top Ten
Zufälliger Snippet
Tech-Ed-Gewinnspiel
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Partner von Codezone.de


Member of Microsoft Community Leader/Insider Program (CLIP)

Threadsichere und generische Kommunikation Windows Forms


Autor: Rainer Hilmer
Sprache: C#
Bewertung:
noch nicht bewertet

Anzahl der Aufrufe: 6648
  

Beschreibung:

Dieses Snippet
http://dotnet-snippets.de/dns/threadsicheres-zuweisen-von-control-eigenschaften-SID829.aspx
gefiel mir so gut daß ich mich entschloß (auch aus gegebenem Anlass), weitere Invoker für andere Bedürfnisse zu schreiben. Mit meiner Klasse ist es möglich, nicht nur Werte in Control-Properties zu schreiben, sondern auch auszulesen. Ausserdem können Methoden von Controls aufgerufen werden. Ich hoffe, Marcel Spies verzeiht mir daß ich seinen PropertyWriter hier verbaut habe.


Abgelegt unter: generics, invoke, thread, control, windows forms.



C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System.Security.Permissions;

[assembly: PermissionSetAttribute(SecurityAction.RequestMinimum, Name = "FullTrust")]
namespace Cyrons.Forms
{
   using System.Reflection;
   using System.Windows.Forms;

   /// <summary>
   /// Bietet Funktionen für den threadsicheren Zugriff auf Objekte von Windows Forms Controls.
   /// </summary>
   public class ControlCommunicationsManager
   {
      #region Delegates

      private delegate void MethodCaller<ControlType>(
         ControlType control,
         string methodName,
         params object[] parameters)
         where ControlType : Control;

      private delegate object PropertyValueReaderCallback<ControlType>(
          ControlType control, string propertyName)
          where ControlType : Control;

      private delegate void PropertyValueWriterCallback<ControlType, PropertyType>(
          ControlType control, string propertyName, PropertyType value)
          where ControlType : Control;

      #endregion Delegates
      #region Methods

      /// <summary>
      /// Invokes a control method call.
      /// </summary>
      /// <typeparam name="ControlType">The type of the control.</typeparam>
      /// <param name="control">The control.</param>
      /// <param name="methodName">Name of the method.</param>
      /// <param name="parameters">Parameters array for the method to be called.</param>
      public void InvokeControlMethodCall<ControlType>(
         ControlType control,
         string methodName, params object[] parameters)
         where ControlType : Control
      {
         if(control.InvokeRequired)
         {
            MethodCaller<ControlType> callerDelegate = InvokeControlMethodCall;
            control.Invoke(callerDelegate, new object[] { control, methodName, parameters });
         }
         else
         {
            MethodInfo method = control.GetType().GetMethod(methodName);
            method.Invoke(control, parameters);
         }
      }

      /// <summary>
      /// Invokes reading of a control property.
      /// </summary>
      /// <typeparam name="ControlType">The type of the control.</typeparam>
      /// <param name="control">The control.</param>
      /// <param name="propertyName">Name of the property.</param>
      /// <returns></returns>
      public object InvokeControlPropertyReader<ControlType>(
         ControlType control,
         string propertyName)
         where ControlType : Control
      {
         if(control.InvokeRequired)
         {
            PropertyValueReaderCallback<ControlType> cb = InvokeControlPropertyReader;
            return control.Invoke(cb, new object[] { control, propertyName });
         }
         PropertyInfo property = control.GetType().GetProperty(propertyName);
         return property.GetValue(control, null);
      }

      /// <summary>
      /// Invokes writing to a control property.
      /// </summary>
      /// <typeparam name="ControlType">The type of the control.</typeparam>
      /// <typeparam name="PropertyType">The type of the property.</typeparam>
      /// <param name="control">The control.</param>
      /// <param name="propertyName">Name of the property.</param>
      /// <param name="value">The value.</param>
      public void InvokeControlPropertyWriter<ControlType, PropertyType>(
          ControlType control,
          string propertyName,
          PropertyType value)
          where ControlType : Control
      {
         if(control.InvokeRequired)
         {
            PropertyValueWriterCallback<ControlType, PropertyType> cb = InvokeControlPropertyWriter;
            control.Invoke(cb, new object[] { control, propertyName, value });
         }
         else
         {
            PropertyInfo property = control.GetType().GetProperty(propertyName);
            property.SetValue(control, value, null);
         }
      }

      #endregion
   }
}
Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)

Rainer Hilmer schrieb am:  03.02.2009 16:46:34

Ich hatte in der Beschreibung ein Anwendungsbeispiel eingefügt. Warum es nicht erscheint, weiß der Geier. Dann eben nochmal hier:
namespace ReadControlFromOtherThread
{
using System;
using System.Threading;
using System.Windows.Forms;
using Cyrons.Forms;

public partial class Form1 : Form
{
#region Fields

private ControlCommunicationsManager ctrlComManager;

#endregion
#region Constructors

public Form1()
{
InitializeComponent();
ctrlComManager = new ControlCommunicationsManager();
}

#endregion Constructors
#region Methods

private void Button_Try_Click(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(MyOtherThread);
}

private void MyOtherThread(object stateInfo)
{
ctrlComManager.InvokeControlMethodCall(TextBox_Input, "AppendText", " bla bla");

ctrlComManager.InvokeControlPropertyWriter(
Label_Output,
"Text",
"Textlänge = "
+ (int)ctrlComManager.InvokeControlPropertyReader(TextBox_Input, "TextLength"));

ctrlComManager.InvokeControlMethodCall(TextBox_Input, "ScrollToCaret", null);
}

#endregion Methods
}
}


Diese Snippets könnten für Sie interessant sein:
[C#] Reflektion und Generika
[C#] Generic Type Creator
[C#] Enum-Extension - Flag in einem Bitfeld gesetzt?
[C#] Threadsichere GUI Änderungen
[C#] Events aus Worker-Thread im GUI-Thread werfen! Kein Invoke!
[C#] Threadsicherer Codeaufruf im Control-Kontext (Invoking)
[C#] Threadsicheres Zuweisen von Control-Eigenschaften
[C#] Control aus anderen Threads aktualisieren
[C#] Invoke Beispiel mittels Erweiterungs Methoden.
[C#] Generischer Invoker für Windows Form Controls
[C#] DelayedEventHandler
[C#] Aktuelles Verzeichnis auslesen
[C#] Asynchrones nachladen von Bildern in DataGridView
[C#] Thread nach Timeout beenden
[VB.NET] CPU Last von mehreren CPUs überwachen
[C#] CPU Last von mehreren CPUs überwachen
[C#] Methode mit Parameter in eigenem Thread starten
[C#] Die Ausführungszeit einer Methode einschränken
[VB.NET] Hängende Processe beenden
[C#] Parallelisierung von Schleifen
[C#] Doppelbufferung für Steuerelemente aktivieren
[C#] Transparenter Hintergrund beim Steuerelement
[C#] CursorChanger
[ASP.net] Formularelement fokusieren auf Ajax Seiten
[C#] Controls gleichen Typs von einem Container in Winform holen
[VB.NET] ControlResize wie in der IDE auch zur Laufzeit
[C#] Eigenschaften aller Steuerlemente eines Formulars setzen
[VB.NET] Alle Textboxen zurücksetzen
[VB.NET] List all controls by using the name of the controls
[ASP.net] bestimmtes Control auf der aspx-Seite finden
[VB.NET] Oft benötigte Properties von Controls schnell zentral ausfüh
[VB.NET] Das Klick-Event von Controls gemeinsam auffangen und zentral
[VB.NET] Linien u. Kreise a la VB6 - Einfach Zeichnen
[C#] iTunes-ProgressBar
[C#] WaterBox
[C#] Custom Toolstrip Renderer
[VB.NET] Alle Controls einer Form Enablen/Disablen
[C#] Löschen aller TextControls auf einer Form in C#
[VB.NET] Control Resize wie in der IDE auch zur Laufzeit - Extended
[ASP.net] NullReferenceException
[C#] Prüfen ob in einem Verzeichnis Schreibrechte verfügbar sind!
[C#] WPF-Steuerelemente in Windows Forms-Anwendung nutzen
[C#] EventFilter
[C#] Form FadeOut
[C#] User-Verwaltung in eine WinForm einbauen

schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.