473,779 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.R esponseEvent += new
WeightReader.Re sponse(this.Rec eiveData);
}
public void ReceiveData(Wei ghtEventArgs args)
{
...
}
}
Because ReceiveData is being called from an external thread, I do the
following before my program is initialized...

System.Windows. Forms.Control.C heckForIllegalC rossThreadCalls = 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()
{
InitializeCompo nent();

this.button1.Cl ick += new EventHandler(bu tton1_Click);
}

void button1_Click(o bject sender, EventArgs e)
{
System.Threadin g.Thread th = new System.Threadin g.Thread(new
System.Threadin g.ThreadStart(t his.Temp));
th.Start();
}

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

Thank you, all.
Trecius
Sep 4 '08 #1
2 1278
Trecius <Tr*****@discus sions.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.C heckForIllegalC rossThreadCalls = 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 BackgroundWorke r.

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

--
Jon Skeet - <sk***@pobox.co m>
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 SetControlPrope rties(){.....}
and then a Delegate like:
Delegate void SetControlPrope rtiesDelegate() ;
Then do myControl.Invok e(new
SetControlPrope rtiesDelegate(S etControlProper ties);

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.Invok eRequired 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.R esponseEvent += new
WeightReader.Re sponse(this.Rec eiveData);
}
public void ReceiveData(Wei ghtEventArgs args)
{
...
}
}
Because ReceiveData is being called from an external thread, I do the
following before my program is initialized...

System.Windows. Forms.Control.C heckForIllegalC rossThreadCalls = 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()
{
InitializeCompo nent();

this.button1.Cl ick += new EventHandler(bu tton1_Click);
}

void button1_Click(o bject sender, EventArgs e)
{
System.Threadin g.Thread th = new System.Threadin g.Thread(new
System.Threadin g.ThreadStart(t his.Temp));
th.Start();
}

void Temp()
{
this.textBox1.V isible = true; // This does not work
//this.textBox1.T ext = "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
2030
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 thread, do I need to use UserControl1.Invoke to set it, or can I just set it? After all, it is only changing a private variable. TIA Charles
9
2257
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 which makes calls to the service. We are using tcp channel which is using binaryformatter by default. The problem is that after a certain number of remoting calls the calls dont get through to the server. The client application makes the call and...
0
898
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 upto 25 clients accessing my server. After that I get an exception on the server , that the system is "unable to find a free thread to complete the requested operation". I know that .Net specifies a default setting of 25 worker threads
1
1706
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 iteration at which point it blows up, everytime. Sample Code: ... Imports System.IO
39
1879
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 spawned thread is finished, but that's not the intended way, because i need that the opc variable change only when the user pushes a key, i think it has something to do with the pthread_attr_set(atribute) options, but i didn't get with the answer...
0
285
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 inside it's "read loop" waiting for data from the client. I find that many times while inside the "read loop" it missed many of the value that was assigned to the public Message variable. For example, the main program send number 1 thru 100, but...
3
252
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 MainForm, does it mean that doProcess is processed inside or outside the thread ? Code from MainForm: Public Class MainForm Private Session As ClientSession = Nothing Private WorkerThread As Threading.Thread = Nothing
3
2353
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 involved are: ASP
13
2683
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 solution. But this question sticks to my head as a conceptual problem) Suppose there are two threads T1, T2 running concurrently:
0
9632
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9471
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10302
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8958
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5501
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4036
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3631
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.