Autor Beitrag
SanPan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16



BeitragVerfasst: So 08.10.06 13:51 
Hey leute,
ich brauch wieder mal den Rat von euch C-Sharp-Gelehrten :)

Ich habe folgendes programmiert... naja, eher mit Hilfe eines Kollegen:

Ich ändere den Text vom Start-Button bei Windows. Ich möchte, dass dort anstatt Start die aktuelle Uhrzeit steht im Format hh:mm. Das klappt auch soweit. Das aktualisieren macht mir probleme. Wenn ich das mit nem Timer oder BAckgroundWorker mache, dann nutzt das Programm zuviel CPU Power. Wie kann man sowas sinnvoll lösen?

greetz,
sanpan69
Spectus.gn
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 38

Win XP
C# (VS 2008 Pro)
BeitragVerfasst: So 08.10.06 17:35 
Also,ich glaube die ganze Sache ist nicht ganz so einfach wie du hoffst.

ausblenden C#-Quelltext
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:
        //muss oben bei using rein
        using System.Runtime.InteropServices;

        //Code zum bekommen der SystemZeit
        SystemTime st = new SystemTime();
        SystemTimeFuncs.GetLocalTime(st);

        // Methode zum Setzen und zu Bekommen der SystemZeit
        public class SystemTimeFuncs
        {
            [DllImport("Kernel32.dll")]
            public static extern void GetLocalTime([In, Out] SystemTime st);
            [DllImport("Kernel32.dll")]
            public static extern bool SetLocalTime([In, Out] SystemTime st);
        }

        // da schreibt der dann (glaue ich) seine bekommenen werte rein
        [StructLayout(LayoutKind.Sequential)]
        public class SystemTime
        {
            public ushort wYear;
            public ushort wMonth;
            public ushort wDayOfWeek;
            public ushort wDay;
            public ushort wHour;
            public ushort wMinute;
            public ushort wSecond;
            public ushort wMilliseconds;
        }


Das müsste der Code zum bekommen der aktuellen SystemZeit sein.

und nun zum ausschalten der CPU-schlacht.

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
       //muss auch oben bei using rein
       using System.Threading;

       //Befehl den Thread für n MilliSeks auf Eis zu legen 
       Thread.Sleep(n);

       //in deinem Fall würde ich sagen (60-st.wSecond(aus dem GetTime)*1000(da Umwandlung von Sek in MilliSek) also:
       Thread.Sleep((60-st.wSecond)*1000);


Hoffe es funzt und du schreibst mir, wenn du fragen hast.

MfG

Carsten

PS.: Wähle das nächste mal bitte ein passenderes Thema.
SanPan Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16



BeitragVerfasst: So 08.10.06 21:23 
Hey Carsten,
schonmal danke soweit. Das mit dem Threading funktioniert soweit recht gut.
Nur er hängt dabei meine Form auf :(
Ich kopier mal schnell meinen gesamten Quelltext:

ausblenden volle Höhe C#-Quelltext
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:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;
using System.Threading;

namespace Start_Again
{
    public partial class sa_form_mainform : Form
    {
        //DLL-Funktionen importieren

            [DllImport("user32")]
                private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
            
            [DllImport("user32")]
                private static extern IntPtr FindWindowEx(IntPtr hWnd1, IntPtr hWnd2, string lpsz1, string lpsz2);
            
            [DllImport("user32")]
                private static extern bool SetWindowText(IntPtr hWnd, string lpString);
            
            [DllImport("user32")]
                private static extern int EnableWindow(IntPtr hWnd, bool bEnable);


        //Eigene Funktionen

            //Funktion zum Ändern des Startbutton-Textes
            public void sa_function_SetStartButtonText(string aText)
            {
                //Window-Handle Speicher
                    IntPtr HWnd;

                //Start-Button suchen
                    HWnd = FindWindow("Shell_TrayWnd", String.Empty);
                    HWnd = FindWindowEx(HWnd, IntPtr.Zero, "Button"null);
                
                //Startbutton-Text ändern
                    SetWindowText(HWnd, aText);
                
                //Refreshen
                    EnableWindow(HWnd, false);
                    EnableWindow(HWnd, true);
            }

            //Funktion zum Refreshen des Start Button Textes
            public void sa_function_RefreshStartButtonText()
            {
                bool x=true;

                while (x)
                {
                    Thread.Sleep(1000);
                    sa_function_SetStartButtonText(DateTime.Now.ToShortTimeString());
                }
            }


        //Funktionsaufrufe

            public sa_form_mainform()
            {
                InitializeComponent();

                //Startbutton-Text aktualisieren
                sa_function_RefreshStartButtonText();
            }
    }
}
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: So 08.10.06 21:31 
Dein erster Ansatz mit einem Timer ist völlig genügend. Du musst dabei aber etwas falsch gemacht haben, wenn du bei einem Intervall von 60000 mehr als 1% CPU verbrätst ;) .

@ Spectus.gn: System.DateTime.Now :gruebel: ?
SanPan Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 16



BeitragVerfasst: So 08.10.06 21:49 
Okay, jetzt muss ich mich schämen. Hab in der Eile in der ich es fertig haben wollte ganz das TimerIntervall vergessen :oops:

Hab ihn jetzt wieder drin und er läuft sogar mit einer Sekunde ohne Probleme.

Thx, Khabarakh.

Dann will ich mich jetzt mal mit den anderen Problemen in dem Mini-Programm auseinandersetzen :D
Spectus.gn
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 38

Win XP
C# (VS 2008 Pro)
BeitragVerfasst: So 08.10.06 21:55 
Zitat:

@ Spectus.gn: System.DateTime.Now ?


Wenn du mir das erklären kannst, wie das gehen soll, dann wäre ich sehr dankbar.
Hab im Netz nur diese Variante gefunden.
Danke schonmal im voraus.

Spectus
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Mo 09.10.06 14:31 
Da Now eine read-only Property ist, lässt sich die aktuelle Systemzeit zwar nur lesen und nicht setzen, aber mehr war ja auch gar nicht gefordert.
Spectus.gn
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 38

Win XP
C# (VS 2008 Pro)
BeitragVerfasst: Mo 09.10.06 14:34 
danke für den Tipp, muss ich mir merken ;)
THX

MfG
Carsten