Winforms and threading
-
Tuesday, May 15, 2012 7:04 PMHello, can someone show me example of windows forms application in which there is one listbox and thread working forever in background, adding items to listbox. I tried something, but i think it's far from right answer.
- Moved by CoolDadTxMVP Tuesday, May 15, 2012 8:58 PM Winforms related (From:Visual C# General)
All Replies
-
Tuesday, May 15, 2012 7:08 PM
Please post the code that you tried, and also why it didn't work. (Did you get errors, if so, what were they. Did it just not do what you wanted, if so, what did it do, etc.)
I'd also suggest looking into the BackgroundWorker class.
- Edited by servy42Microsoft Community Contributor Tuesday, May 15, 2012 7:08 PM
-
Tuesday, May 15, 2012 7:13 PM
Can you check-out below thread
http://social.msdn.microsoft.com/Forums/en/winforms/thread/26149f70-c5a5-44ee-89c1-73025226904f
Regards,
Ahmed Ibrahim
SQL Server Setup Team
This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" and "Vote as Helpful" on posts that help you.
This can be beneficial to other community members reading the thread. -
Tuesday, May 15, 2012 7:30 PM
I'm not sure about complexion of this issue. I want to let my background thread have biggest priority in whole process. I want to make server application. In the beginning it should make thread for organizing every connection and write logs in Listbox. I have problem because i don't know where can i make new thread which would have access to Form1.Listbox1. This is what i tried:
public class ServerLoop
public partial class Form1 : Form { public Thread tServerLoop; public ServerLoop serverLoop; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { console.Items.Clear(); players.Items.Clear(); players.Items.Add("Witaj w serwerze"); addConsoleMessage("test"); serverLoop = new ServerLoop(this); tServerLoop = new Thread(serverLoop.loop); tServerLoop.Start(); } private void connectButton_Click(object sender, EventArgs e) { } public void addConsoleMessage(String msg) { console.Items.Add(msg); } }
And Form1 class:
{ Form1 form1; public ServerLoop(Form1 f) { form1 = f; } public void loop() { form1.addConsoleMessage("test"); } } -
Tuesday, May 15, 2012 7:38 PM
You neglected to mention the problem that you're having. In general that is important information that you should include; you shouldn't have us guess at what the problem is.
Your code is mostly correct, but you're not marshaling the code back to the UI thread from the background thread, so you're getting a cross thread exception. See the link provided earlier in this thread for some example solutions that deal with this problem.
- Edited by servy42Microsoft Community Contributor Tuesday, May 15, 2012 7:38 PM
-
Wednesday, May 16, 2012 11:25 AM
> I want to let my background thread have biggest priority in whole process.
Even over the UI thread? if so, don't bother with threading at all and let the UI get blocked while your code runs.
> I want to make server application.
Do you mean a Windows Service? Or something that will run on a server machine? In both cases, you'd be far better off starting with a console application. Services [generally] cannot display forms, so your form will eventually have to be removed anyway.
> In the beginning it should make thread for organizing every connection and write logs in Listbox. I have problem because i don't know where can i make new thread which would have access to Form1.Listbox1.
You have two problems there - when to spin up a new thread and how to update the Listbox across threads.
I have no idea how you do the first one - that's your application logic.
The second, however, is better understood. Create a method on the Form that adds to the listbox. The create a delegate for this method. Then, in your thread, create an instance of the delegate method and use listbox.Invoke() to call it, something like:
class Form1 : Form { private delegate void AddToListDelegate( string entry ); public void AddToList( string entry ) { // InvokeRequired will be true when running from another thread if ( this.lstLogs.InvokeRequired ) { // Create a delegate to this method ... AddToListDelegate method = new AddToListDelegate( AddToList ); // ... and call it (I'm a bit rusty on this bit) this.lstLogs.Invoke( method, new object[] { entry } ); } else { // If we're on the same (UI) thread, just update the Control directly this.lstLogs.Items.Add( entry ) ; } } private ListBox lstLogs ; }
Regards, Phill W.
- Marked As Answer by pioncze Thursday, May 17, 2012 8:56 AM


