Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 18th, 2014, 10:25 AM   #1
RareDevil
Freelance programmer
 
RareDevil's Avatar
 
Join Date: Oct 2008
Location: Denmark
Posts: 56
Rep Power: 9 RareDevil is on a distinguished road
Send a message via MSN to RareDevil
Threading Problems

I'm using winforms, so to explain my problem as simple as i can.
The problem is that I'm starting a background thread and in that thread im calling a method in the main thread that should change some text in a label, but the thread is closing properly and don't give any errors, but it does not change to text in the label.
The idea is that i want to change the label text from the background thread i have started but since nothing happens i have hit a dead end on how to do this.
I really hope that someone can help me with finding a solution to the problem quickly since this program I'm trying make is a exam assignment that i have to be done with within the next 10 days.

Here is the code i have made:
C# Syntax (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Text.RegularExpressions;
  11. using System.Net.Sockets;
  12. using System.Net;
  13. using System.Threading;
  14. using System.Diagnostics;
  15. using System.Globalization;
  16.  
  17. namespace Server
  18. {
  19. public partial class Server : Form
  20. {
  21. public static string ClientAddr;
  22. public static int PortNum;
  23.  
  24. public Server()
  25. {
  26. InitializeComponent();
  27. }
  28.  
  29. // This method adds a new line to the status text makes sure that the status text dont get over 12 lines.
  30. public void AddStatusText(string newLine)
  31. {
  32.  
  33. string Text = StatusText.Text;
  34. Text += "\n" + newLine;
  35. string[] lines = Regex.Split(Text, "\n");
  36. string ReturnText = "";
  37.  
  38. if (lines.Length <= 12)
  39. {
  40. for (int i = 0; i < lines.Length; i++)
  41. {
  42. if (i == 0)
  43. ReturnText = lines[i];
  44. else
  45. ReturnText += "\n" + lines[i];
  46. }
  47. }
  48. else
  49. {
  50. int lineNumbers = lines.Length;
  51. int newStartLine = lineNumbers - 12;
  52.  
  53. for (int i = newStartLine; i < lines.Length; i++)
  54. {
  55. if (i == newStartLine)
  56. ReturnText = lines[i];
  57. else
  58. ReturnText += "\n" + lines[i];
  59. }
  60. }
  61.  
  62. StatusText.Text = ReturnText;
  63. }
  64.  
  65. //This method calls the AddStatusText method, the reason is made like this is so that i can be called from another class.
  66. public static void CallAddStatusText(string line)
  67. {
  68. Server temp = new Server();
  69. temp.AddStatusText(line);
  70. }
  71.  
  72. // This method makes sure that its a valid ip adresse that have been entered
  73. public static bool IpCheck(string IpAddr)
  74. {
  75. try
  76. {
  77. System.Net.IPAddress ipAddress = System.Net.IPAddress.Parse(IpAddr);
  78. return true;
  79. }
  80. catch
  81. {
  82. return false;
  83. }
  84. }
  85.  
  86. // This method makes sure that the entered port is a valid port - There may need to made more to this method.
  87. public static bool PortCheck(string Port)
  88. {
  89. try
  90. {
  91. int PortNum = Convert.ToInt32(Port);
  92. if (PortNum >= 0 && PortNum <= 65535)
  93. return true;
  94. else
  95. return false;
  96. }
  97. catch
  98. {
  99. return false;
  100. }
  101. }
  102.  
  103. // This is the Start Server button it starts the hole server process
  104. private void RunServer_Click(object sender, EventArgs e)
  105. {
  106. if (!IpCheck(IpAdd.Text))
  107. MessageBox.Show("The IP adresse is invalid", "ERROR");
  108. else if (!PortCheck(Port.Text))
  109. MessageBox.Show("The port number is invalid", "ERROR");
  110. else
  111. {
  112. IpAdd.ReadOnly = true;
  113. Port.ReadOnly = true;
  114. RunServer.Enabled = false;
  115. //ClientAddr = IpAdd.Text;
  116. PortNum = Convert.ToInt32(Port.Text);
  117. AddStatusText("The server is starting up.");
  118.  
  119. //From this point on im testing some treading stuff.
  120. NetworkHandler networkHandler = new NetworkHandler();
  121. Thread NetworkHandlerThread = new Thread(new ThreadStart(networkHandler.Handler));
  122. NetworkHandlerThread.Name = "BackgroundNetworkHandler";
  123. NetworkHandlerThread.IsBackground = true;
  124. //NetworkHandlerThread.
  125. NetworkHandlerThread.Start();
  126.  
  127. if (NetworkHandlerThread.IsAlive)
  128. {
  129. AddStatusText("The tread is alive.");
  130. }
  131. else
  132. {
  133. AddStatusText("The tread is dead.");
  134. }
  135. }
  136. }
  137. }
  138.  
  139. class NetworkHandler
  140. {
  141. public string IpAdd = Server.ClientAddr;
  142. public int PortNum = Server.PortNum;
  143. // This method is handling the network communication
  144. public void Handler()
  145. {
  146. Server.CallAddStatusText("It got this far.....");
  147. }
  148.  
  149. public string ChangeData(string dataString)
  150. {
  151. return dataString;
  152. }
  153. }
  154. }
__________________
<html> <head> <title> hello world</title></head> <body> <div id="cookie"> Hello world </div> <div id="php"> <?php echo "hello world" ?> </div> </body> </html>
RareDevil is offline   Reply With Quote
Old Mar 18th, 2014, 2:55 PM   #2
mykhayl
Newbie
 
mykhayl's Avatar
 
Join Date: Feb 2013
Location: Milano
Posts: 8
Rep Power: 0 mykhayl is on a distinguished road
Re: Threading Problems

You cannot modify GUI object from another thread. You must invoke the change on the Form main thread.
Use this code:

string newText = "abc"; // running on worker thread
someLabel.Invoke((MethodInvoker)delegate {
    someLabel.Text = newText; // runs on UI thread
});

someLabel is a generic GUI Object.
__________________
"There's no point in being exact about something if you don't even know what you're talking about."

JOHN VON NEUMANN
mykhayl is offline   Reply With Quote
Old Mar 19th, 2014, 5:23 AM   #3
RareDevil
Freelance programmer
 
RareDevil's Avatar
 
Join Date: Oct 2008
Location: Denmark
Posts: 56
Rep Power: 9 RareDevil is on a distinguished road
Send a message via MSN to RareDevil
Re: Threading Problems

after trying multiple things and ended up with destroying Visual Studio i have decided to not make my program with threading. So i do not need any help with this at the moment, but i may try doing something like this again at some other time, but for now i have to little experience with C# so it will take to long time for me to find and implement a working solution, so i have decided to go back to my first idea.

Thanks for the help mykhayl, i found it quite useful but i could not really find out how to get it to work tho sadly
__________________
<html> <head> <title> hello world</title></head> <body> <div id="cookie"> Hello world </div> <div id="php"> <?php echo "hello world" ?> </div> </body> </html>
RareDevil is offline   Reply With Quote
Old Mar 19th, 2014, 9:13 AM   #4
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 771
Rep Power: 11 King is on a distinguished road
Re: Threading Problems

Without having background threads on your UI, when you do any type of long running task your UI will look like it has hung. I would strongly recommend trying to get what mykhayl suggested to work. If you can't get it working, post back with code and the errors you are getting.
__________________
I am Addicted to Linux!
King is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
mathematical Problems kemeia C++ 10 Mar 6th, 2013 9:44 PM
jar problems billra Community Introductions 0 Feb 19th, 2011 1:14 PM
Crashes and problems with multi-threaded Lua host (C++) dag10 C++ 2 Apr 1st, 2010 10:17 PM
problems loading 2 dlls in Delphi7 nico765 Delphi 0 Jan 7th, 2006 4:03 PM
New Switch, FTP Problems ViOLATiON Coder's Corner Lounge 6 Sep 13th, 2005 2:44 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:03 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC