Autor Beitrag
Soc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 49



BeitragVerfasst: Do 19.11.09 09:45 
Hallo Zusammen,

mein VS gibt folgende Warning aus wenn ich die Suspend und Resume Methode verwende:

Warnung 1 "System.Threading.Thread.Suspend()" ist veraltet: "Thread.Suspend has been deprecated. Please use other classes in System.Threading, such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect resources.

Ich komme da auch in der Literatur nicht unbedingt weiter.
Hat jemand vieleicht ein Beispiel ?

(Wir benötigt um in einen Windows Service der Worker schlafen zu legen.)

Gruß

Soc
danielf
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 1012
Erhaltene Danke: 24

Windows XP
C#, Visual Studio
BeitragVerfasst: Do 19.11.09 10:56 
Hallo,

also ich finde gleich mehrere Quellen die Aussagen warum diese Funktionen veraltet sind.

Zum Beispiel: msdn.microsoft.com/e....thread.suspend.aspx

Caution:
Do not use the Suspend and Resume methods to synchronize the activities of threads. You have no way of knowing what code a thread is executing when you suspend it. If you suspend a thread while it holds locks during a security permission evaluation, other threads in the AppDomain might be blocked. If you suspend a thread while it is executing a class constructor, other threads in the AppDomain that attempt to use that class are blocked. Deadlocks can occur very easily.

...

D.h. du könntest mit Thread.suspend einen Zustand generieren der deine ganze Anwendung lahm legt oder undefiniertes Verhalten hervorruft. Deshalb sollst du alternative Synchronisierungsmöglichkeiten verwenden. Dafür kannst du unter anderem Locks, Monitor oder Events verwenden. (msdn.microsoft.com/e...ibrary/ms173179.aspx

Um zu entscheiden was in deinem Fall das Richtige ist, benötigt man mehr Informationen. Ich Vermute bei deinem Problem reicht eine Funktion wie worker.Pause, in dem einfach eine Klassenvariable auf false gesetzt wird, schon aus.

Gruß Daniel


Bsp:
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:
bool m_pause = false;
public void Pause()
{
   m_pause = true;  
}

public void Resume()
{
   m_pause = false;  
}

public void Start()
{
   Produce();
}

public void Produce()
{
   while(true)
{
 if (m_pause == false)
{
   ProduceOutput();
}
}
}
Soc Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 49



BeitragVerfasst: Do 19.11.09 12:21 
Hallo Daniel,

das funktioniert leider bei mir nicht, da der Worker in einer anderen Klasse ist.

Code:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
        protected override void OnStart(string[] args)
        {
            base.OnStart(args);

            if (args.Length != 0)
                this.logFile = args[0];

            ssSERVC scheduler = new ssSERVC(this.logFile);
            thread = new Thread(new ThreadStart(scheduler.StartWorker));
            thread.Start();
            this.EventLog.WriteEntry("SnapShotGUI - Scheduler Service wurde gestartet.");
        }


Worker:
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:
30:
    class ssSERVC
    {
        private string logFile = Application.StartupPath + @"\ssSERV.log";

        /// <summary>
        /// Konstruktor
        /// </summary>
        /// <param name="log">Path mit Logname</param>
        public ssSERVC(string logFile)
        {
            if (logFile != "")
                this.logFile = logFile;
        }
        // Startmethode des Threads
        public void StartWorker()
        {
            try
            {
                while (true)
                {
                    string entry = "[" + DateTime.Now.ToShortDateString() + " - " + DateTime.Now.ToLongTimeString() + "] ich warte...";
                    WriteToLogFile(entry);
                    System.Threading.Thread.Sleep(10000);
                }
            }
            catch (Exception e)
            {
                EventLog.WriteEntry("SnapShotGUI""Fehler im Scheduler: " + e, EventLogEntryType.Error);
            }
        }


Damit komme ich nicht einen einen Schalter dran.

Gruß

Soc