Posts mit Tag: c#

Listen filtern mit LINQ – c# Snippets

Damit die Reihe c# Snippets gleich mit zwei Artikel startet folgt nun noch ein kurzes aber praktisches Snippet.

Problemstellung
Gegeben ist eine Liste.
Gesucht ist eine Teilmenge der Liste, die eine bestimmte Bedingung erfüllt.

Beispiel
Gegeben: List<int> list = new List<int>() { 5, 3, 17, 2, 23 };
Gesucht ist eine neue Liste, die alle Elemente größer 10 enthält.

View Code CSHARP
1
2
List<int> list = ...;
List<int> result = (from i in list where i > 10 select i).ToList();

In dem Select-Teil kann auch Code ausgeführt werden, zum Beispiel wenn die Quadrate der Zahlen gesucht sind:

View Code CSHARP
1
2
List<int> list = ...;
List<int> result = (from i in list where i > 10 select i * i).ToList();

SerializeToString – c# Snippets

Dieser Artikel ist der erste Teil der Reihe c# Snippets.

Oftmals hat man eine Liste von Elementen möchte diese aber als einen String darstellen.

Beispiel
Gegegen ist eine Liste von decimal-Werten:
List<decimal>list = new list<decimal>{ 1.05, 2.50; 3.66 };
Gewünscht ist "1,05; 2,50; 3,66"

Der herkömmliche Ansatz
… sieht dann vermutlich so oder so ähnlich aus:

View Code CSHARP
1
2
3
4
5
6
StringBuilder sb = new StringBuilder();
foreach (decimal d in list) {
    sb.Append(d.ToString("0.00") + ";  ");
}
if (sb.Length > 0) sb.Length -=2; // Trennzeichen am Ende entfernen
return sb.ToString();

Die Hilfsmethode SerializeToString mit Lamda-Ausdrücken

View Code CSHARP
1
2
3
4
5
6
7
8
9
static class StringExtensions
{
    public static string SerializeToString<T>(this IEnumerable<T> list, Func<T, string> serialize, string separator)
    {
        StringBuilder sb2 = list.Aggregate(new StringBuilder(), (sb, item) => sb.Append(serialize(item) + separator));
        if (sb2.Length > 0) sb2.Length -= separator.Length;
        return sb2.ToString();
    }
}

Der Aufruf von SerializeToString
ist nun wesentlich kürzer und meiner Meinung nach besser lesbar:

View Code CSHARP
1
string serializedValues = list.SerializeToString(d => d.ToString("0.00"));

Natürlich funktioniert dies auch für Listen von komplexeren Datenstrukturen und auch verschachtelt:

View Code CSHARP
1
2
3
4
5
6
7
List<List<DateTime>> list = ...;
 
string serializedValues = list.SerializeToString(i1 => i1.Count + " Elemente: " + i1.SerializeToString(i2 => i2.ToString(), ", "), "\r\n");
// Beispiel-Ausgabe:
// 1.1.2010, 2.1.2010
// 1.1.2011, 2.1.2011, 3.1.2011
// ...

c# – WorkingSetControl / RamControl: Displaying the Working Set size / current RAM usage of an application

English
Did you ever want to display the Working Set size / current RAM usage of your c# application? If the answer to this question is ‘yes’, here is your solution. If the answer is ‘no’ you still might find some usable code snippets in the control.

Deutsch
Es ist relativ einfach die aktuelle Speicherbelegung in einem c# Programm herauszufinden. Ich habe dies Funktionalität in ein kleinen Control verpackt.

Screenshot des Controls:
WorkingSetControl

View Code CSHARP
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
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
using System.Drawing;
 
namespace MyProject
{
    /// <summary>
    /// Control to display the  current WorkingSet size. Aka "RamControl". Just call Start to start the monitoring.
    /// </summary>
    class WorkingSetControl : Control
    {
        Thread t1;
        bool active = false;
 
        public WorkingSetControl()
        {
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        }
 
        /// <summary>
        /// Start displaying the current WorkingSet size. If you don't call Start you won't see a thing!
        /// </summary>
        internal void Start()
        {
            // start only if not started yet
            if (t1 == null)
            {
                // although we could use a lambda expression to create this thread don't use one to be .NET 2.0 compatible.
                t1 = new Thread(run);
 
                // if you *HAVE* to use a lambda expression, fine have fun with your cryptic code:
                /*
                t1 = new Thread(() =>
                {
                    while (active)
                    {
                        Invalidate();
                        Thread.Sleep(100);
                    }
                });
                */
 
                // set active to true, so our run method does actually run in loops 'n loops 'n loops until active is set to false ==> see Dispose.
                active = true;
                t1.Start();
            }
        }
 
        void run()
        {
            while (active)
            {
                Invalidate();
                Thread.Sleep(100);
            }
        }
 
        /// <summary>
        /// Get and draw the WorkingSet size
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            string ram = "xxx";
 
            // Only get the WorkingSet size if the control is in active mode.
            // This prevents the control displaying the WorkingSet size of Visual Studio in the designer (which works actually, but is somehow confusing).
            if (active)
            {
                long lngSessMemory = Process.GetCurrentProcess().WorkingSet64;
                ram = (Convert.ToDouble(lngSessMemory) / 1000 / 1000).ToString("0.00");
            }
 
            // do the grphics stuff
            e.Graphics.Clear(BackColor);
            e.Graphics.DrawString("WorkingSet: " + ram + " MB", Font, new SolidBrush(ForeColor), 0, 0);
        }
 
        /// <summary>
        /// Lets make sure our thread terminates at some point in time.
        /// </summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing)
        {
            // I know we should wait for the thread to actually funish by using t1.Join() but that would slow down this methob by up to 100ms.
            // If you have any Problems with controls not disposing properly, let me know and send me a solution ;)
            active = false;
            base.Dispose(disposing);
        }
    }
}

Dotfuscator CE + Explizite Schnittstellen- implementierung (Interface) = TypeLoadException: Die Methode GetEnumerator hat keine Implementierung

Ich verwende Dotfuscator CE (Community Edition) um meine .Net Anwendungen zu schützen.

Kürzlich ist dabei ein Fehler aufgetreten:

TypeLoadException: Die Methode GetEnumerator hat keine Implementierung

Das Problem bestand aber nur nachdem die Anwendung vom Dotfuscator bearbeitet wurde. Vorher funktionierte alles noch prima.

Aufgetreten war das Ganze, weil ich in einer Klasse zwei verschiedene IEnumerable<T> implementiert hatte.
Beispiel:

View Code CSHARP
1
2
3
class MyClass : IEnumerable<ClassA>, IEnumerable<ClassB> {
...
}

In diesem Fall ist eine sogenannte Explizite Schnittstellenimplementierung notwendig. Dies hat der Dotfuscator offensichtlich nicht verstanden. Nachdem ich die Implementierung der Schnittstelle in eine extra Methode ausgelagert hatte und die Explizite Schnittstellenimplementierung entfernt hatte funktionierte alles wieder gut.

Windows XP SP3 + Visual Studio 2010 (c#) = Bluescreen

Nachdem ich angefangen habe einen Teil meiner Projekte in von VS 2008 nach VS 2010 zu konvertieren musste ich Visual Studio 2010 auch auf allen Entwicklungsrechnern installieren. Die Migration der Projekte und build-skripte verlief erstaunlich problem- und reibungslos.

Probleme gab es nur auf dem letzten Windows XP PC. Visual Studio konnte zwar problemlos geöffnet, das Projekt auch gebaut und gestartet werden. Wenn VS 2010 allerdings geschlossen wurde verabschiedete sich Windows XP jedes Mal ausnahmslos mit einem Bluescreen.

Sofortlösung
Die schnellste Lösung war Visual Studio 2010 nicht zu schließen sondern mit dem Task Manager zu beenden. Da dies allerdings nur eine Notlösung war musste ich weiter probieren…

Das Installieren der von VS 2010 angemahnten “Windows-Automatisierungs-API” brachte leider nichts.

Endgültige Lösung
Da VS 2010 WPF (Windows Presentation Foundation) verwendet und diese Hardwarebeschleunigung verwendet beschloss ich das Problem mit einem Grafikkartrenteiberupdate anzugehen. Nahezu unglaublich aber wahr: Der aktuelle GeForce Treiber (197.45) löste das Problem. VS lässt sich nun wieder ohne Absturz schließen.

Es gilt also:
Windows XP SP3
+ Visual Studio 2010 (c#)
+ Neuer Grafikkarten Treiber = :-) (Zumindest in meinem Fall)

c# – eigenes Control, Keys.Up, Down, Left, Right: OnKeyDown / OnKeyPress vs. ProcessCmdKey

Wer ein eigenes Control (Steuerelement) in c# erstellt und dachte, dass er mit OnKeyDown Pfeiltasten erkennen kann ist auf dem Holzweg:

View Code CSHARP
1
2
3
4
5
6
protected override void OnKeyDown(KeyEventArgs e)
{
	/* wird für Pfeiltasten nicht aufgerufen */
	base.OnKeyDown(e);
	if (e.KeyCode == Keys.Up) MoveIndex(-1);
}

Bei Druck auf die Pfeiltasten (Keys.Up, Keys.Down, Keys.Left, Keys.Right) wird weder OnKeyDown noch OnKeyPress aufgerufen.

Die Lösung bringt ProcessCmdKey:

View Code CSHARP
1
2
3
4
5
6
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
	if (keyData == Keys.Up) MoveIndex(-1);
 
	return base.ProcessCmdKey(ref msg, keyData);
}

Hier muss allerdings beachtet werden, dass in keyData auch Strg, Shift und Alt kodiert werden. keyData == Keys.Up ist nur true, wenn keine Steuerungstaste (Strg, Shift, Alt) gleichzeitig gedrückt wurde.

Welche Tasten gedrückt sind lässt sich folgendermaßen herausfinden:

View Code CSHARP
1
2
3
4
bool shift = (keyData & Keys.Shift) != 0;
bool control = (keyData & Keys.Control) != 0;
bool alt = (keyData & Keys.Alt) != 0;
Keys unmodifiedKey = (keyData & Keys.KeyCode);

c#: Win32Exception: Fehler beim Erstellen des Fensterhandles

Wann tritt diese Exception auf? – Wenn zu viele Steuerelemente erstellt und nicht wieder aufgeräumt wurden.

Beispiel

View Code CSHARP
1
2
3
4
5
6
7
8
9
10
11
12
    public partial class Form1 : Form
    {
        void ForceException()
        {
            while (true)
            {
                Label l = new Label();
                this.Controls.Add(l);
                this.Controls.Clear();
            }
        }
    }

Bei jedem Schleifendurchlauf wird ein neues Label erstellt. mit this.Controls.Clear(); wird das Label zwar entfernt, aber das Fenster-Handle des Labels existiert weiterhin. Da jeder Prozess nur begrenzt viele Fenster-Handles haben kann sind diese irgendwann aufgebraucht.

Abhilfe

View Code CSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
    public partial class Form1 : Form
    {
        void ForceException()
        {
            while (true)
            {
                Label l = new Label();
                this.Controls.Add(l);
                this.Controls.Clear();
                l.Dispose();
            }
        }
    }

Mit l.Dispose(); werden alle Ressourcen, die das Label reserviert hatte freigegeben. Damit wird auch das Fenster-Handle wieder frei und kann später wieder verwendet werden. Die obige while-Schleife läuft beliebig lange ohne Fehler.

C#: Strg+C, Strg+V, Strg+X

In einer benutzerfreundlichen Anwendung sollte der Benutzer wenn möglich Strg+C, Strg+V und Strg+X verwenden können.

Dies ist einfach zu realisieren:

View Code CSHARP
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
class MyForm : Form
{
	protected override void OnKeyDown(KeyEventArgs e)
	{
		if (e.Modifiers == Keys.Control)
		{
			switch (e.KeyCode)
			{
				case Keys.C:
					tryCopy();
					break;
				case Keys.V:
					tryPaste(); 
					break;
				case Keys.X:
					tryCut();
					break;
				case Keys.Delete:
					tryDelete();
					break;
				case Keys.Right:
					tryMoveNext();
					break;
				case Keys.Left:
					tryMovePrevious();
					break;
			}
		}
		base.OnKeyDown(e);
	}
}

Nun noch die tryXY-Funktionen implementieren und fertig ist die Tastaturbedienung des Programms.

TextControl

In c#.Net gibt es einen Label. Dieser ist recht praktisch, solange man nicht weitere Steuerelemente direkt anschließend platzieren möchte.

Die einzige Möglichkeit einen Label zu realisieren, der überhaupt kein Padding hat, ist diesen selbst zu schreiben.

View Code CSHARP
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
class TextControl : Control
{
	private Brush brush = new SolidBrush(Color.Black);
	private StringFormat stringFormat;
	private Graphics graphics = null;
 
	internal static StringFormat GetStringFormat()
	{
		StringFormat stringFormat = new StringFormat();
		stringFormat.Alignment = StringAlignment.Center;
		stringFormat.LineAlignment = StringAlignment.Near;
		stringFormat.Trimming = StringTrimming.None;
		stringFormat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.MeasureTrailingSpaces;
		return stringFormat;
	}
 
	public TextControl()
	{
		this.stringFormat = GetStringFormat();
		graphics = this.CreateGraphics();
		this.TabStop = false;
	}
	~TextControl()
	{
		graphics.Dispose();
	}
 
	protected override void OnPaint(PaintEventArgs e)
	{
		base.OnPaint(e);
		RePaint();
	}
	void RePaint()
	{
		Graphics g = this.CreateGraphics();
		g.Clear(this.BackColor);
		g.DrawString(base.Text, base.Font, brush, base.ClientRectangle, stringFormat);
	}
	protected override void OnParentChanged(EventArgs e)
	{
		base.OnParentChanged(e);
		if (this.AutoSize)
		{
			DoAutoSize();
		}
	}
 
	[System.ComponentModel.Browsable(true)]
	[System.ComponentModel.Category("Layout")]
	public override bool AutoSize
	{
		get { return base.AutoSize; }
		set { base.AutoSize = value; }
	}
 
	protected override void OnForeColorChanged(EventArgs e)
	{
		base.OnForeColorChanged(e);
		brush.Dispose();
		brush = new SolidBrush(this.ForeColor);
	}
 
	protected override void OnTextChanged(EventArgs e)
	{
 
		base.OnTextChanged(e);
		if (this.AutoSize &amp;&amp; this.Parent != null)
		{
			DoAutoSize();
		}
	}
	void DoAutoSize()
	{
		RectangleF rf = MeasureText(base.Text, graphics, stringFormat, base.Font);
		this.Size = new Size((int)rf.Width, (int)rf.Height);
	}
 
	public static RectangleF MeasureText(string text, Graphics g, StringFormat sf, Font font)
	{
		if (text.Length &gt; 0)
		{
			sf.SetMeasurableCharacterRanges(new CharacterRange[] { new CharacterRange(0, text.Length) });
			Region[] stringRegions = g.MeasureCharacterRanges(text, font, new RectangleF(0, 0, 1000, 100), sf);
			return stringRegions[0].GetBounds(g);
		}
		else
		{
			return new RectangleF(new PointF(0, 0), new SizeF(0, 0));
		}
	}
	public static float getTextLength(string Text, Graphics g, StringFormat sf, Font font)
	{
		RectangleF rf = MeasureText(Text, g, sf, font);
		return rf.Width;
	}
 
	public static int GetMaxTextByLength(string text, int maxLength, Graphics g, StringFormat sf, Font font)
	{
		if (getTextLength(text, g, sf, font) &lt;= maxLength)
		{
			return text.Length;
		}
		string[] parts = text.Split(new char[] { ' ' }, StringSplitOptions.None);
		int len = 0;
		string current = parts[0];
		len = (int)getTextLength(current, g, sf, font);
		if (len &gt; maxLength) return 0;
		int n = 1;
		while (len &lt; maxLength)
		{
			current += " " + parts[n];
			n++;
			len = (int)getTextLength(current, g, sf, font);
		}
		return len;
 
	}
 
	protected override void OnAutoSizeChanged(EventArgs e)
	{
		base.OnAutoSizeChanged(e);
		this.OnTextChanged(e);
	}
 
}

WordPress Themes