473,378 Members | 1,522 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,378 software developers and data experts.

Problem with thread changing UI

Hello, Newsgroupians:

I have a question regarding threads changing the UI of a form. I have an
external device that sends signals to my application by using their library.
To do this, all I need to do is set the event that handles the incoming data.

public class Reader
{
private WeightReader m_reader;
...
public Reader()
{
this.m_reader.ResponseEvent += new
WeightReader.Response(this.ReceiveData);
}
public void ReceiveData(WeightEventArgs args)
{
...
}
}
Because ReceiveData is being called from an external thread, I do the
following before my program is initialized...

System.Windows.Forms.Control.CheckForIllegalCrossT hreadCalls = false;

To make a long story short, in my ReceiveData(), I check the arguments that
come in and change my UI accordingly. I can set the text of a control
without any problem; however, if at any time a control needs to have its
Visible property set, the program just hangs. How can I get around this?

Here's a sample application that I have written; it only has a button and a
textbox. Initially, the textbox's Visible property is set to false when
intialized. Overall, this small application indicates the problem I am
having that an external thread cannot set the Visible property of a control;
however, it has the ability to set the text of a control. Why's this?

public partial class TempFrm : Form
{
public TempFrm()
{
InitializeComponent();

this.button1.Click += new EventHandler(button1_Click);
}

void button1_Click(object sender, EventArgs e)
{
System.Threading.Thread th = new System.Threading.Thread(new
System.Threading.ThreadStart(this.Temp));
th.Start();
}

void Temp()
{
this.textBox1.Visible = true; // This does not work
//this.textBox1.Text = "Hello"; // This works if textBox1 is INITIALLY
visible
}
}

Thank you, all.
Trecius
Sep 4 '08 #1
2 1263
Trecius <Tr*****@discussions.microsoft.comwrote:

<snip>
Because ReceiveData is being called from an external thread, I do the
following before my program is initialized...

System.Windows.Forms.Control.CheckForIllegalCrossT hreadCalls = false;
Don't do that. It's the programmatic equivalent of covering your ears
and saying "I can't hear you!" when someone tells you off. You should
use Control.Invoke/BeginInvoke instead, or a BackgroundWorker.

See http://pobox.com/~skeet/csharp/threads/winforms.shtml

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 4 '08 #2
You should call the Invoke method on a control to update it when not on the
thread that created it. You need to make a method like
void SetControlProperties(){.....}
and then a Delegate like:
Delegate void SetControlPropertiesDelegate();
Then do myControl.Invoke(new
SetControlPropertiesDelegate(SetControlProperties) ;

That will then call that function on the thread that created the control. If
you might not be on a different thread, you can call check
myControl.InvokeRequired to see if you need to do that.

Search the MSDN for help on it.
--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"Trecius" wrote:
Hello, Newsgroupians:

I have a question regarding threads changing the UI of a form. I have an
external device that sends signals to my application by using their library.
To do this, all I need to do is set the event that handles the incoming data.

public class Reader
{
private WeightReader m_reader;
...
public Reader()
{
this.m_reader.ResponseEvent += new
WeightReader.Response(this.ReceiveData);
}
public void ReceiveData(WeightEventArgs args)
{
...
}
}
Because ReceiveData is being called from an external thread, I do the
following before my program is initialized...

System.Windows.Forms.Control.CheckForIllegalCrossT hreadCalls = false;

To make a long story short, in my ReceiveData(), I check the arguments that
come in and change my UI accordingly. I can set the text of a control
without any problem; however, if at any time a control needs to have its
Visible property set, the program just hangs. How can I get around this?

Here's a sample application that I have written; it only has a button and a
textbox. Initially, the textbox's Visible property is set to false when
intialized. Overall, this small application indicates the problem I am
having that an external thread cannot set the Visible property of a control;
however, it has the ability to set the text of a control. Why's this?

public partial class TempFrm : Form
{
public TempFrm()
{
InitializeComponent();

this.button1.Click += new EventHandler(button1_Click);
}

void button1_Click(object sender, EventArgs e)
{
System.Threading.Thread th = new System.Threading.Thread(new
System.Threading.ThreadStart(this.Temp));
th.Start();
}

void Temp()
{
this.textBox1.Visible = true; // This does not work
//this.textBox1.Text = "Hello"; // This works if textBox1 is INITIALLY
visible
}
}

Thank you, all.
Trecius
Sep 4 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Charles Law | last post by:
I have a user control created on the main thread. Let's say, for arguments sake, that it has a single property that maintains a private variable. If I want to set that property from a worker...
9
by: Sudesh Sawant | last post by:
Hello, We have an application which communicates using remoting. There is a server which is a Windows Service. The server exposes an object which is a singleton. The client is a Web Application...
0
by: Gary | last post by:
Hi, I am developing a remote server application. this has to be service multiple requests at the same time. This is currently being done using the ..Net thread pool. I have no problems with...
1
by: Dominic Marks | last post by:
Hello Group, I'm bemused as to why this code isn't working. It's a very simple loop over the array of FileInfo objects returned from DirectoryInfo.GetFiles. It works fine until the eigth...
39
by: elnanni | last post by:
I've a problem in the use of threads, they don't work as i want them to. Here's the code, the problem is that if i uncomment the //pthread_join(thread_ID, NULL);, the main program stops until the...
0
by: fiefie.niles | last post by:
I am having problem with thread. I have a Session class with public string variable (called Message) that I set from my Main program. In the session class it checks for the value of Message while...
3
by: fiefie.niles | last post by:
I am using a thread. Inside the thread there is a sub ThreadMain. I am calling the thread from the main program MainForm. Inside the thread, if I am calling a sub (say doProcess) from the...
3
by: Simon Dean | last post by:
Hi, I believe I have a website (I didn't do the original coding) which uses JavaScript/ASP to generate cookies. It's a shopping cart application called UCart I believe. The technologies...
13
by: AliRezaGoogle | last post by:
Dear Members, I have a problem in the concepts of Monit.Enter and Monitor.Exit (Before everything I should say that I know how to solve this problem by using Monitor.Wait and I do not need a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.