Autor Beitrag
Dingo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 64
Erhaltene Danke: 1



BeitragVerfasst: Di 27.06.17 09:16 
Grüße!

Beschäftige mich gerade mit Threads die Parameter beinhalten und habe diesen Code:

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:
class Program {
        static void Main(string[] args) {

            ThreadStart del = new ThreadStart(TestMethod1);
            ThreadStart del2 = new ThreadStart(TestMethod2);

            Thread thread1 = new Thread(del);
            Thread thread2 = new Thread(new ParameterizedThreadStart(del2));
            thread1.Start();
            thread2.Start();
            
            for (int i = 0; i <= 100; i++) {
                for (int k = 1; k <= 200; k++) {
                    Console.Write("Y");
                }
                Console.ReadLine();
            }
            
        }

        public static void TestMethod1() {
            for (int i = 0; i <= 100; i++) {
                for (int k = 1; k <= 20; k++) {
                    Console.Write("X");
                }
                Console.WriteLine("Sekundär-Thread 1 " + i);
            }
        }

        public static void TestMethod2() {
            for (int i = 1; i <= 100; i++) {
                for (int k = 1; k <= 30; k++) {
                    Console.Write("Z");
                }
                Console.WriteLine("Sekundär-Thread 2 " + i);
            }
        }

    }


Leider wird mir folgendes als falsch angekreidet:

ausblenden C#-Quelltext
1:
Thread thread2 = new Thread(new ParameterizedThreadStart(del2));					


Finde leider den Fehler nicht, könnt ihr mir hier weiter helfen?

Danke schonmal! :)
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Di 27.06.17 09:19 
Wie ist denn die Fehlermeldung?

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Dingo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 64
Erhaltene Danke: 1



BeitragVerfasst: Di 27.06.17 09:54 
Folgende Fehlermeldung gibt er aus:

ausblenden C#-Quelltext
1:
Keine Überladung für "ThreadStart" stimmt mit dem Delegaten "System.Threading.ParameterizedThreadStart" überein.					
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4764
Erhaltene Danke: 1052

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Di 27.06.17 10:20 
ParameterizedThreadStart erwartet ja auch eine Methode mit einem Parameter (object), also z.B.
ausblenden C#-Quelltext
1:
2:
3:
public static void TestMethod2(object data) 
{
}

Und den Aufruf des Threads entsprechend anpassen:
ausblenden C#-Quelltext
1:
2:
Thread thread2 = new Thread(new ParameterizedThreadStart(TestMethod2));
thread2.Start(42);


PS: Das manuelle Aufrufen von Threads solltest du aber nur als Übung auffassen. Ansonsten verwendet man die Klasse Task und/oder asynchrone Methoden...
Dingo Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 64
Erhaltene Danke: 1



BeitragVerfasst: Di 27.06.17 12:41 
OK, verstehe, danke! :)