Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > .NET Languages
You last visited: Today at 21:52

  • Please register to post and access all features, it's quick, easy and FREE!

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.

Closed Thread
 
Old 07/15/2013, 11:54   #16
 
.Stefan's Avatar
 
elite*gold: 12
Join Date: Jun 2009
Posts: 2,621
Received Thanks: 1,239
Quote:
Originally Posted by tolio View Post
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 :/
.Stefan is offline  
Old 07/15/2013, 12:34   #17
 
Kraizy​'s Avatar
 
elite*gold: 0
The Black Market: 471/0/0
Join Date: Apr 2010
Posts: 9,696
Received Thanks: 1,811
Zeig uns mal den gesamten Code, wo du den Thread startest etc.
Kraizy​ is offline  
Old 07/15/2013, 13:15   #18
 
.Stefan's Avatar
 
elite*gold: 12
Join Date: Jun 2009
Posts: 2,621
Received Thanks: 1,239
Quote:
Originally Posted by Kraizy​ View Post
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";
        }

    }
}
.Stefan is offline  
Old 07/15/2013, 13:25   #19
 
Kraizy​'s Avatar
 
elite*gold: 0
The Black Market: 471/0/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.
Kraizy​ is offline  
Old 07/15/2013, 14:46   #20
 
.Stefan's Avatar
 
elite*gold: 12
Join Date: Jun 2009
Posts: 2,621
Received Thanks: 1,239
Quote:
Originally Posted by Kraizy​ View Post
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 ?
.Stefan is offline  
Old 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;  
            }
        }
qkuh is offline  
Old 07/15/2013, 15:43   #22



 
Shawak's Avatar
 
elite*gold: 0
The Black Market: 259/0/0
Join Date: Apr 2010
Posts: 10,289
Received Thanks: 3,613
Quote:
Originally Posted by .Stefan View Post
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";
            });
        }
    }
}
Shawak is offline  
Old 07/15/2013, 16:34   #23
 
Schlüsselbein's Avatar
 
elite*gold: 0
Join Date: Feb 2013
Posts: 1,137
Received Thanks: 869
Schlüsselbein is offline  
Old 07/15/2013, 19:44   #24
 
.Stefan's Avatar
 
elite*gold: 12
Join Date: Jun 2009
Posts: 2,621
Received Thanks: 1,239
Quote:
Originally Posted by qkuh View Post
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 View Post
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 View Post
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 ?
.Stefan is offline  
Old 07/15/2013, 22:28   #25



 
Shawak's Avatar
 
elite*gold: 0
The Black Market: 259/0/0
Join Date: Apr 2010
Posts: 10,289
Received Thanks: 3,613
Kombinier einfach das was ich geschrieben habe mit dem von Schlüsselbein.
Shawak is offline  
Old 07/15/2013, 22:33   #26
 
.Stefan's Avatar
 
elite*gold: 12
Join Date: Jun 2009
Posts: 2,621
Received Thanks: 1,239
Quote:
Originally Posted by Shawak View Post
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 ?
.Stefan is offline  
Old 07/16/2013, 01:56   #27
 
tolio's Avatar
 
elite*gold: 2932
The Black Market: 169/1/0
Join Date: Oct 2009
Posts: 6,966
Received Thanks: 1,097
übergib referenzen zu den objekten an die neuen klassen
tolio is offline  
Old 07/16/2013, 07:46   #28
 
.Stefan's Avatar
 
elite*gold: 12
Join Date: Jun 2009
Posts: 2,621
Received Thanks: 1,239
Quote:
Originally Posted by tolio View Post
übergib referenzen zu den objekten an die neuen klassen
Könntest du mir ein kleines Beispiel geben :s ?
.Stefan is offline  
Old 07/17/2013, 09:00   #29
 
'Aleo's Avatar
 
elite*gold: 175
Join Date: Jun 2013
Posts: 157
Received Thanks: 44
Quote:
Originally Posted by .Stefan View Post
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#.
'Aleo is offline  
Thanks
1 User
Old 07/17/2013, 09:41   #30



 
Shawak's Avatar
 
elite*gold: 0
The Black Market: 259/0/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
Shawak is offline  
Thanks
1 User
Closed Thread


Similar Threads 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.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.