Autor Beitrag
coolace
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 141



BeitragVerfasst: Do 26.08.10 10:26 
Hy,

ich komme gerade nicht weiter und google hat auch keine passenden
Antworten für mein Problem gefunden.

Ich will gern meine Speicherfunktion als Thread aufrufen

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:
       List<Autos> speicherelist = new List<Autos>();

       public void Speichern()
       {
            ParameterizedThreadStart pts = new ParameterizedThreadStart(handleThread);
            Thread t = new Thread(pts);
              t.Start(speicherelist);
       }
          

       public static void handleThread(List<Autos>  speichertlist)
        {
             try
            {
               
                BinaryFormatter binf = new BinaryFormatter();
                FileStream fs = new FileStream(@"C:\daten.dat", FileMode.Create);
                binf.Serialize(fs,speichertlist);
                fs.Close();                
                speichertlist.Clear();
            }
            catch (Exception)
            {
                
            }
        }


Mein erstes Problem ist er lässt mich die Liste nicht übergeben. Grund: new ParameterizedThreadStart(handleThread);
Der Compiler meint, keine Überladung für handleThread stimmt mit den Delegaten System.Threadthing.ParamterizedThreadStart überein.

Mein 2 Problem, ich bin beim googeln über die Seite geflogen, www.david-tielke.de/?p=159, hier behandelt er das
Thema Threadting und damit auftretende sporadische Fehler, die ich beim Speichern nicht brauchen kann.

Wie müsste ich das richtig machen ?

Danke und Gruß

Coolace
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4807
Erhaltene Danke: 1061

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Do 26.08.10 11:08 
Deine Methode bei ParameterizedThreadStart darf nur 'object' als Parameter haben:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
public static void handleThread(object par)
{
  List<Autos> list = par as List<Autos>;
  if(list != null)
  {
    ...
  }
  else
    throw new Exception("expected List<Autos> as parameter!");
}


Und bzgl. Threadsynchronisation:
Wenn du mehrere Threads hast, die gleichzeitig ein Objekt beschreiben, dann mußt du das Schreiben synchronisieren (neben den im Artikel erwähnten WaitHandle, Semaphoren, Mutex, ... schau dir mal das C#-Schlüsselwort 'lock' an).
Trashkid2000
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 561
Erhaltene Danke: 137



BeitragVerfasst: Do 26.08.10 11:17 
Habe auch noch eine Lösung, die, so finde ich, sehr elegant ist. Zumal die Exceptions dann wirklich im Hauptthread landen.

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
public void Speichern()
{
  Thread t = new Thread(() =>
  {
    try
    {
      BinaryFormatter binf = new BinaryFormatter();
      using (FileStream fs = new FileStream(@"C:\daten.dat", FileMode.Create))
      {
        binf.Serialize(fs, speicherelist);    
      }
      speicherelist.Clear();    
    }
    catch (Exception ex)
    { 
      //mache was im Fehlerfall
    }
  });
}


LG, MArko
coolace Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 141



BeitragVerfasst: Do 26.08.10 11:28 
Hy,

Danke für eure Hilfe, das war genau was ich gesucht habe.

Gruß

Coolace