473,405 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Can't update a listbox from other class with it's own thread

2
I can't update a listbox from other class with it's own thread.
My code:
The main class:
Expand|Select|Wrap|Line Numbers
  1. <....>
  2.     public partial class mainForm : Form
  3.     {
  4.         <....>
  5.         private delegate void AddMessageToLogHandler(object sender, AddMessageToLogArgs e);
  6.         private object _sender = Thread.CurrentThread;
  7.         private AddMessageToLogArgs _e;
  8.  
  9.         public mainForm()
  10.         {
  11.                 <....>
  12.                 _e = new AddMessageToLogArgs("Network thread created", 0);
  13.                 networkThread = new Thread(new ThreadStart(StartNetwork));
  14.                 networkThread.Start();
  15.                 AddMessageToLog(_sender, _e);
  16.                 <....>
  17.         }
  18.  
  19.         private void StartNetwork()
  20.         {
  21.                 <....>
  22.                 tcpListener = new TcpListener(ipAddress, connectionPort);
  23.                 tcpListener.Start();
  24.                 _e = new AddMessageToLogArgs("Connected to network", 0);
  25.                 AddMessageToLog(_sender, _e);
  26.  
  27.                 while (true)
  28.                 {
  29.                     client = new Client(tcpListener.AcceptTcpClient());
  30.                 }
  31.                 <....>
  32.         }
  33.  
  34.         public void AddMessageToLog(object sender, AddMessageToLogArgs e)
  35.         {
  36.             if (this.InvokeRequired)
  37.             {
  38.                 AddMessageToLogHandler addMessageToLog = new AddMessageToLogHandler(AddMessageToLog);
  39.                 this.BeginInvoke(addMessageToLog, new object[] { sender, e });
  40.             }
  41.             else
  42.             {
  43.                 switch (e.ColorNumber)
  44.                 {
  45.                     case 0:
  46.                         logListBox.ForeColor = Color.Black;
  47.                         logListBox.Items.Add(DateTime.Now + ": SUCCESS: " + e.LogMessage + "\n");
  48.                     <....>
  49.                 }
  50.             }
  51.         }
  52.         <....>
  53.     }
  54.  
  55.     public class AddMessageToLogArgs : EventArgs
  56.     {
  57.         public string LogMessage;
  58.         public int ColorNumber;
  59.  
  60.         public AddMessageToLogArgs(string localLogMessage, int localColorNumber)
  61.         {
  62.             this.LogMessage = localLogMessage;
  63.             this.ColorNumber = localColorNumber;
  64.         }
  65.     }
  66.  
  67. The other class:
  68. <....>
  69.     class Client
  70.     {
  71.         <....>
  72.         private mainForm mainFormClass = new mainForm();
  73.         object _sender = Thread.CurrentThread;
  74.         AddMessageToLogArgs _e;
  75.  
  76.         public Client(TcpClient tcpClient)
  77.         {
  78.             <....>
  79.         }
  80.  
  81.         private void ReceiveMessage(IAsyncResult asyncResult)
  82.         {
  83.             <....>
  84.                             _e = new AddMessageToLogArgs("Server got message" + messageReceived, 2);
  85.                             mainFormClass.AddMessageToLog(_sender, _e);
  86.             <....>
  87.         }
  88.         <....>
  89.     }
  90. <....>
Everything works fine - no any compile errors. Listbox gets updated from the main class, but from the client class - it doesn't. I know it's because of the "private mainForm mainFormClass = new mainForm();". This code instantiates a new mainForm class, but I need to access to already instantiated mainForm class. But how to do that? I've tried ti make function AddMessageToLogArgs a static one, but then I can't access from it the listbox. So I'm stuck here.
Dec 16 '07 #1
2 1816
Shashi Sadasivan
1,435 Expert 1GB
Wherever you are creating the Client Class from the main class, send the reference of the main clas to the client class

So try chaging the following
client = new Client(tcpListener.AcceptTcpClient());

to this
client = new Client(tcpListener.AcceptTcpClient(), this);

where "this" is the reference of the main class (you would have to add a maiform variable in the Client class and assign it the value sent by the main class)

So when you have to change data, instead of creating a new class (which is a complete new object to the current main form) directly use this variable to change data displayed !
Dec 16 '07 #2
Wisher
2
Thank you very much, you are a life saver :) Sure it works and it's so obvious that it makes shame on me that I couldn't think of it myself :)
Dec 16 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Hal Vaughan | last post by:
First, I am aware of both SwingUtilities.invokeLater(), and of using Thread to create a new thread.  These are part of the problem. I want to have something running in the background, while the...
2
by: Luna | last post by:
Hi, I want to pass the values that have been chosen from a few comboboxes into a listbox as a single entry. how do i do this?? this is what i tried... it will probably give u some...
1
by: Chris Capel | last post by:
Ok, so I have a ListBox. So it has these Items. These Items are of type SummaryItem class SummaryItem { public SummaryItem(String prefix) {Prefix = prefix;} public String Value; public String...
13
by: Jason Jacob | last post by:
To all, I have a GUI program (use c#), and I have create a Thread for loading some bulk data, I also arrange the GUI program like this: 1) load a form showing "Wait for loading..." etc 2) a...
3
by: Sushil Srivastava | last post by:
Hi Guys, Would you be able to help me using C# GUI (with user interface component) in my MFC application. I have used managed extension, COM-interops, etc but problem is this C# component has...
10
by: Dica | last post by:
i've used threads in a couple of c# desktop apps with no problems before, but can't seem to get this code to update my listBox: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As...
8
by: =?Utf-8?B?R3JlZyBMYXJzZW4=?= | last post by:
I'm trying to figure out how to modify a panel (panel1) from a backgroundworker thread. But can't get the panel to show the new controls added by the backgroundwork task. Here is my code. In...
3
by: rn5a | last post by:
I am not very sure whether I should have continued with my earlier thread or started a new thread whose subject matter was somewhat similar to the subject matter in this thread. Anyway after much...
2
by: rrflore2 | last post by:
Ok. I'm writing and deleting to an xml file using a dataset. I have a function in my codebehind page that binds a listbox to the dataset that performs the writes/deletes. Everything seems to be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.