Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Empfohlene Antworten

Veröffentlicht

Hallo,

Ich habe die Aufgabe in der Schule bekommen ein Programm meiner Wahl zu programmieren, jedoch habe ich das Problem, das ich 2 Sachen auf einer Windows Form gleichzeitig machen möchte. Eine Frage soll erscheinen und im Hintergrund soll eine Picture Box hin und her wandern. Und das Gleichzeitig.

Dann habe ich mich an die Klasse Threading gewagt aber komme nicht weiter.

Hier ein kleiner Auszug

public void btnStart_Click(object sender, EventArgs e)

{

ThreadStart neben;

neben = new ThreadStart(MyProcedure);

Thread myFirstThread = new Thread(neben);

// den zweiten Thread starten

myFirstThread.Start();

}

public static void MyProcedure()

{

int x1 = pctPosition.Location.X, y1 = pctPosition.Location.Y;

Point p; //Punkt p wird deklariert

p = pctPosition.Location;

while (x1 < 915)

{

x1 = pctPosition.Location.X;

y1 = pctPosition.Location.Y;

p.X += 5;

p.Y += 0;

pctPosition.Location = p;

System.Threading.Thread.Sleep(200);

}

while (y1 > 550)

{

x1 = pctPosition.Location.X;

y1 = pctPosition.Location.Y;

p = pctPosition.Location;

p.X += 0;

p.Y -= 5;

pctPosition.Location = p;

System.Threading.Thread.Sleep(200);

}

}

}

}

Nun erscheint aber der Fehler:

Error 1 An object reference is required for the non-static field, method, or property 'Taxi.Route1.pctPosition' C:\Users\Murat\Documents\Visual Studio 2008\Projects\Taxi\Taxi\Route1.cs 45 22 Taxi

Wäre nett wenn mir einer Weiterhelfen könnte!

Mfg

Du solltest MyProcedure() nicht als static deklarieren.

Aber sei gewarnt: Die Single-Thread-Rule verbietet den Zugriff auf Steuerelemente aus Threads die nicht der GUI-Thread sind! Und du verstößt dagegen. Das wird nicht gut gehen.

Alternativer Ansatz: Nimm einen BackgroundWorker, der die Berechnungen macht und nutze das ProgressChanged-Event, um die PictureBox zu bewegen, denn ProgressChanged wird autom. im GUI-Thread ausgeführt.

Infos zum BgW hier: Übersicht über die BackgroundWorker-Komponente

Grüße, JasonDelife.

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.