|
You last visited: Today at 21:52
Advertisement
[c#] Frage zu Thread's / Threading
Discussion on [c#] Frage zu Thread's / Threading within the .NET Languages forum part of the Coders Den category.
07/15/2013, 11:54
|
#16
|
elite*gold: 12
Join Date: Jun 2009
Posts: 2,621
Received Thanks: 1,239
|
Quote:
Originally Posted by tolio
wenn du das genau so aufrufst dann friert natürlich deine hauptform(/der thread dieser) ein und nicht der thread den du neu gestartet hast, wenn du den einfrieren willst musst es halt im kontext des neu erstellten threads aufrufen
|
Sorry, ich kann dir momentan nicht ganz folgen. Aber ich denke, dass ich das grundsätzlich verstanden habe, was du meinst. Wie kann ich denn das so einstellen, dass genau der Thread gestopt wird ( Thread.Sleep(2000) ) und nicht die Hauptform. Eigentlich steht. Thread.sleep ja in der Methode des Thread's :/
|
|
|
07/15/2013, 12:34
|
#17
|
elite*gold: 0
Join Date: Apr 2010
Posts: 9,696
Received Thanks: 1,811
|
Zeig uns mal den gesamten Code, wo du den Thread startest etc.
|
|
|
07/15/2013, 13:15
|
#18
|
elite*gold: 12
Join Date: Jun 2009
Posts: 2,621
Received Thanks: 1,239
|
Quote:
Originally Posted by Kraizy
Zeig uns mal den gesamten Code, wo du den Thread startest etc.
|
Okay.
Funktioniert :
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;
namespace ui_threading
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Task t1 = new Task(Task1);
t1.RunSynchronously();
}
private void Task1()
{
label1.Text = "12";
}
}
}
Form wird erst nach den 5 Sekunden angezeigt.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Threading.Tasks;
namespace ui_threading
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Task t1 = new Task(Task1);
t1.RunSynchronously();
}
private void Task1()
{
label1.Text = "12";
Thread.Sleep(5000);
label1.Text = "1234";
}
}
}
|
|
|
07/15/2013, 13:25
|
#19
|
elite*gold: 0
Join Date: Apr 2010
Posts: 9,696
Received Thanks: 1,811
|
Hab die Task Klasse zwar noch nicht benutzt, aber allein der Name RunSynchronously sagt doch schon alles.
|
|
|
07/15/2013, 14:46
|
#20
|
elite*gold: 12
Join Date: Jun 2009
Posts: 2,621
Received Thanks: 1,239
|
Quote:
Originally Posted by Kraizy
Hab die Task Klasse zwar noch nicht benutzt, aber allein der Name RunSynchronously sagt doch schon alles.
|
Okay, ich habe den Code nun geändert zu :
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace ui_threading
{
public partial class Form1 : Form
{
private delegate void UpdateStatusDelegate(string status);
public void UpdateStatus(string status)
{
if (InvokeRequired)
BeginInvoke(new UpdateStatusDelegate(InnerUpdateStatus), new object[] { status });
else
InnerUpdateStatus(status);
}
private void InnerUpdateStatus(string status)
{
label1.Text = status;
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Task t1 = new Task(Task1);
t1.Start();
}
private void Task1()
{
UpdateStatus("Text1");
Thread.Sleep(5000);
UpdateStatus("Text2");
}
}
}
Damit funktioniert alles soweit. Jedoch scheint mir das mit den 2 Methoden für das Label etwas aufwendig. Aber möglich. Kennt jemand bessere Methoden ?
|
|
|
07/15/2013, 15:06
|
#21
|
elite*gold: 1000
Join Date: Apr 2012
Posts: 1,003
Received Thanks: 208
|
Du kannst es auch so machen:
Code:
private void setEnabled(string control, bool setEnabled)
{
Button btn = (Button)Controls.Find(control, true)[0];
if (btn.InvokeRequired)
{
btn.Invoke((MethodInvoker)delegate
{
btn.Enabled = setEnabled;
});
}
else
{
btn.Enabled = setEnabled;
}
}
|
|
|
07/15/2013, 15:43
|
#22
|
elite*gold: 0
Join Date: Apr 2010
Posts: 10,289
Received Thanks: 3,613
|
Quote:
Originally Posted by .Stefan
Okay, ich habe den Code nun geändert zu :
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace ui_threading
{
public partial class Form1 : Form
{
private delegate void UpdateStatusDelegate(string status);
public void UpdateStatus(string status)
{
if (InvokeRequired)
BeginInvoke(new UpdateStatusDelegate(InnerUpdateStatus), new object[] { status });
else
InnerUpdateStatus(status);
}
private void InnerUpdateStatus(string status)
{
label1.Text = status;
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Task t1 = new Task(Task1);
t1.Start();
}
private void Task1()
{
UpdateStatus("Text1");
Thread.Sleep(5000);
UpdateStatus("Text2");
}
}
}
Damit funktioniert alles soweit. Jedoch scheint mir das mit den 2 Methoden für das Label etwas aufwendig. Aber möglich. Kennt jemand bessere Methoden ?
|
geht viel einfacher:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace ui_threading
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
System.Threading.Tasks.Task.Factory.StartNew(() =>
{
Label1.Text = "Text1";
Thread.Sleep(5000);
Label1.Text = "Text2";
});
}
}
}
|
|
|
07/15/2013, 16:34
|
#23
|
elite*gold: 0
Join Date: Feb 2013
Posts: 1,137
Received Thanks: 869
|
|
|
|
07/15/2013, 19:44
|
#24
|
elite*gold: 12
Join Date: Jun 2009
Posts: 2,621
Received Thanks: 1,239
|
Quote:
Originally Posted by qkuh
Du kannst es auch so machen:
Code:
private void setEnabled(string control, bool setEnabled)
{
Button btn = (Button)Controls.Find(control, true)[0];
if (btn.InvokeRequired)
{
btn.Invoke((MethodInvoker)delegate
{
btn.Enabled = setEnabled;
});
}
else
{
btn.Enabled = setEnabled;
}
}
|
Sieht auch nicht gerade einfacher aus ^^
Quote:
Originally Posted by Shawak
geht viel einfacher:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace ui_threading
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
System.Threading.Tasks.Task.Factory.StartNew(() =>
{
Label1.Text = "Text1";
Thread.Sleep(5000);
Label1.Text = "Text2";
});
}
}
}
|
Bist du dir sicher, dass dies so funktioniert. Bei mir kommt immer noch der Fehler : Ungültiger threadübergreifender Vorgang: Der Zugriff auf das Steuerelement lbl_test erfolgte von einem anderen Thread als dem Thread, für den es erstellt wurde.
Quote:
Originally Posted by Schlüsselbein
|
Okay muss ich mir näher ansehen.
Danke für die Antworten.
Ich hätte eine weitere kleine Frage : Wie kann ich eine 2. Klasse erstellen, die Methoden beinhaltet, die auch z.B Label's ändern kann ?
|
|
|
07/15/2013, 22:28
|
#25
|
elite*gold: 0
Join Date: Apr 2010
Posts: 10,289
Received Thanks: 3,613
|
Kombinier einfach das was ich geschrieben habe mit dem von Schlüsselbein.
|
|
|
07/15/2013, 22:33
|
#26
|
elite*gold: 12
Join Date: Jun 2009
Posts: 2,621
Received Thanks: 1,239
|
Quote:
Originally Posted by Shawak
Kombinier einfach das was ich geschrieben habe mit dem von Schlüsselbein.
|
Okay das funktioniert wunderbar
Jedoch eine weitere kleine Frage :
Wie kann ich eine 2. Klasse erstellen, die Methoden beinhaltet, die auch z.B Label's ändern kann ?
|
|
|
07/16/2013, 01:56
|
#27
|
elite*gold: 2932
Join Date: Oct 2009
Posts: 6,966
Received Thanks: 1,097
|
übergib referenzen zu den objekten an die neuen klassen
|
|
|
07/16/2013, 07:46
|
#28
|
elite*gold: 12
Join Date: Jun 2009
Posts: 2,621
Received Thanks: 1,239
|
Quote:
Originally Posted by tolio
übergib referenzen zu den objekten an die neuen klassen
|
Könntest du mir ein kleines Beispiel geben :s ?
|
|
|
07/17/2013, 09:00
|
#29
|
elite*gold: 175
Join Date: Jun 2013
Posts: 157
Received Thanks: 44
|
Quote:
Originally Posted by .Stefan
Könntest du mir ein kleines Beispiel geben :s ?
|
Code:
void ChangeText(Label Control, string text)
{
Control.Text = text;
}
Wäre eine Möglichkeit in C#.
|
|
|
07/17/2013, 09:41
|
#30
|
elite*gold: 0
Join Date: Apr 2010
Posts: 10,289
Received Thanks: 3,613
|
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace ui_threading
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
System.Threading.Tasks.Task.Factory.StartNew(() =>
{
this.Invoke(() =>
{
Label1.Text = "Text1";
Thread.Sleep(5000);
Label1.Text = "Text2";
});
});
}
}
}
Sollte funktionieren
Ansonsten;
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace ui_threading
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
System.Threading.Tasks.Task.Factory.StartNew(() =>
{
this.Invoke(() { Label1.Text = "Text1"; });
Thread.Sleep(5000);
this.Invoke(() { Label1.Text = "Text2"; });
});
}
}
}
Lg
|
|
|
 |
|
Similar Threads
|
C++ Threading
03/01/2013 - C/C++ - 3 Replies
Ich habe da mal eine frage zu Threading in C++
Wie kann ich eine Thread Array starten?
ich weiß das ich in C# es z.B. so machen kann:)
Thread myThread;
myThread= new myThread;
|
Threading
02/22/2013 - Nostale - 3 Replies
Hi!I have a question for what i need multithreading i developing game server.Soo i know that i need this for connection.
|
C# Cross Threading
10/13/2012 - .NET Languages - 5 Replies
I was working on Console Applications for a long time and i was able to edit the console from almost everywhere in the project; However, when i migrated to windows forms applications i wondered that i can't edit for example a textBox from any other class because it tells me some error about cross threading
So can any one help me ?
|
C++ Threading
03/24/2012 - CO2 Programming - 13 Replies
Anybody got some good threading tutorials in c++?
#Edit
Using boost would be fine?
|
Threading
11/18/2011 - CO2 Private Server - 10 Replies
What should I do?
A thread for every map that's used by players, a global thread for all players or a single thread for every player.
|
All times are GMT +1. The time now is 21:53.
|
|