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

GUI Thread in C#

13
Hi there,

Iam trying to make an app that has about 14 different threads which have to access the GUI and update some of the labels. Now if i do that, after a little whil it kinda hangs, I think its becaus the gui has been accesed by two or more threads at the same time. So i tried to solve this writing this code:

Expand|Select|Wrap|Line Numbers
  1.         private void SetText(string text, Label item)
  2.         {
  3.                 if (item.InvokeRequired)
  4.                 {
  5.                     lock (myLockObject)
  6.                     {
  7.                         SetTextCallback d = new SetTextCallback(SetText);
  8.                         this.Invoke(d, new object[] { text, item });
  9.                     }
  10.                 }
  11.  
  12.                 else
  13.                 {
  14.                     lock (myLockObject2)
  15.                     {
  16.                         item.Text = text;
  17.                     }
  18.                 }
  19.  
  20.         }
The problem is when i trie to run this it will still hang becaus there are two different lock objects which both try to get to the gui. I also tried this:

Expand|Select|Wrap|Line Numbers
  1.         private void SetText(string text, Label item)
  2.         {
  3.                 if (item.InvokeRequired)
  4.                 {
  5.                     lock (myLockObject)
  6.                     {
  7.                         SetTextCallback d = new SetTextCallback(SetText);
  8.                         this.Invoke(d, new object[] { text, item });
  9.                     }
  10.                 }
  11.  
  12.                 else
  13.                 {
  14.                     lock (myLockObject)
  15.                     {
  16.                         item.Text = text;
  17.                     }
  18.                 }
  19.  
  20.         }
Here the whole program hangs, i think its because there is one lock object thats being used for two different codes, i also tried this:

Expand|Select|Wrap|Line Numbers
  1.         private void SetText(string text, Label item)
  2.         {
  3.             lock (myLockObject)
  4.             {
  5.                 if (item.InvokeRequired)
  6.                 {
  7.                         SetTextCallback d = new SetTextCallback(SetText);
  8.                         this.Invoke(d, new object[] { text, item });
  9.                 }
  10.  
  11.                 else
  12.                 {
  13.                         item.Text = text;
  14.                 }
  15.             }
  16.  
  17.         }
And this also makes the app hang =(. So my question is how to solve this? or do i rly have to use a background worker on updating labels/texboxes in the gui?

Heres an example of the time being updated in the gui:

Expand|Select|Wrap|Line Numbers
  1.         private void tijd()
  2.         {
  3.             while (stoppen == 0)
  4.             {
  5.                 System.Threading.Thread.Sleep(500);              
  6.                 SetText(DateTime.Now.ToString(),lbl_tijd);
  7.             }
  8.         }
Thx in advance =D!
May 14 '08 #1
10 5371
IanWright
179 100+
If I need regular updates on screen then normally I have a separate thread running in the background which updates the screen at set intervals based upon variable parameters that it has access to. Obviously these will need locking if you're changing them from multiple threads at the same time.

I always use delegates when doing this to prevent the GUI locking up, which I believe is similar to your callback functions, so I think you're almost there.

Just to note, I found that in one application the lock() stopped working. Having read up on it, it isn't reliable for large numbers of repetitive, successive calls (e.g. drawing), so just be careful when you are using it.

Ian
May 14 '08 #2
bobido
13
If I need regular updates on screen then normally I have a separate thread running in the background which updates the screen at set intervals based upon variable parameters that it has access to. Obviously these will need locking if you're changing them from multiple threads at the same time.

I always use delegates when doing this to prevent the GUI locking up, which I believe is similar to your callback functions, so I think you're almost there.

Just to note, I found that in one application the lock() stopped working. Having read up on it, it isn't reliable for large numbers of repetitive, successive calls (e.g. drawing), so just be careful when you are using it.

Ian
thx m8 =D. I tried another way, by putting all the data in an array and have one thread update the lbls with the data in the array. That didnt work =,(. So iam goin to try and use the backgroundworker, which I read should be easier to work with (as long as it works for me =p)

greetz, bobido!
May 14 '08 #3
Plater
7,872 Expert 4TB
Are you calling those functions with an event or just trying to call it directly from other threads?
May 14 '08 #4
bobido
13
Are you calling those functions with an event or just trying to call it directly from other threads?
They are being called on an event =)
May 14 '08 #5
iLL
63
I didn't really look to close at your code, but I think you need a global Mutex lock

Example:

Expand|Select|Wrap|Line Numbers
  1.  
  2. class blah
  3. {
  4.     System.Threading.Mutex mLock;
  5.  
  6.     public blah()
  7.     {
  8.         mLock = new System.Threading.Mutex
  9.     }
  10.  
  11.     public void method1()
  12.     {
  13.         mLock.WaitOne();
  14.         // Critical Section
  15.         mLock.Release();
  16.     }
  17.  
  18.     public void method2()
  19.     {
  20.         mLock.WaitOne();
  21.         // Critical Section
  22.         mLock.Release();
  23.     }
  24. }
  25.  
Only one thread will be allowed into ether critical sections at a time
May 14 '08 #6
iLL
63
After taking a little closer look at your code. I don't see how it is hanging. Here we should only worry about race conditions, and there should not be anything hanging on a race condition. Deadlocks and starvations causes hangups. Does it hang up at the same spot every time? Does it work for one or two threads? Or does it not work at all?
May 14 '08 #7
bobido
13
After taking a little closer look at your code. I don't see how it is hanging. Here we should only worry about race conditions, and there should not be anything hanging on a race condition. Deadlocks causes hangups. Does it hang up at the same spot every time? Does it work for one or two threads? Or does it not work at all?
It does work with two threads. Iam using an DDE connection to an server, through a hot link. Whenever, for example, the time is being update and information comes streaming in on the same time, the dde connection will hang because it cant wait with updating (i think thats becaus it isnt a thread but an eventhandler??). Cant i just isolate parts of my gui so that some part of the code can always access those labels, =(? Iam goin to try to make an ReaderWriterLock becaus the gui only needs to read the data anyway, but than the problem may still occur when two hot links activate a event on the same time. Iam running out of ideas, lol =p
May 14 '08 #8
iLL
63
It does work with two threads. Iam using an DDE connection to an server, through a hot link. Whenever, for example, the time is being update and information comes streaming in on the same time, the dde connection will hang because it cant wait with updating (i think thats becaus it isnt a thread but an eventhandler??). Cant i just isolate parts of my gui so that some part of the code can always access those labels, =(? Iam goin to try to make an ReaderWriterLock becaus the gui only needs to read the data anyway, but than the problem may still occur when two hot links activate a event on the same time. Iam running out of ideas, lol =p
I hate to tell you this, but I have no experience with dealing dynamic data exchange.

But let me see of I have this correct.

You have some data coming through the buffer. When the data comes in, it triggers an event. When this event is triggered, some thread will update the label. Is that correct?

And you are using 14 threads so that you can read the data fast enough?
May 14 '08 #9
iLL
63
Cant i just isolate parts of my gui so that some part of the code can always access those labels
If every thread has access to your FormX class, I don't see why you can't make a method that updates a label. Only one thread can do this at a time.

You can also make a label public. This might be bad though.

Iam goin to try to make an ReaderWriterLock becaus the gui only needs to read the data anyway, but than the problem may still occur when two hot links activate a event on the same time.
You don't need any lock if you are only reading. As for two hot links activating an event at the same time, is that possible?
May 14 '08 #10
bobido
13
finally...

I managed to solve the problem:

Expand|Select|Wrap|Line Numbers
  1.         public class TheContainer
  2.         {
  3.             public static Main TheForm;
  4.             public static DdeClient TheDdeClient;
  5.         }
By putting the form and the dde connection in a container? It worked :S?

I dont know why, can anyone tell me please =)?
May 20 '08 #11

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

Similar topics

14
by: adeger | last post by:
Having trouble with my first forays into threads. Basically, the threads don't seem to be working in parallel (or you might say are blocking). I've boiled my problems to the following short code...
4
by: Gilles Leblanc | last post by:
Hi I have started a small project with PyOpenGL. I am wondering what are the options for a GUI. So far I checked PyUI but it has some problems with 3d rendering outside the Windows platform. I...
7
by: Ivan | last post by:
Hi I have following problem: I'm creating two threads who are performing some tasks. When one thread finished I would like to restart her again (e.g. new job). Following example demonstrates...
4
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the...
5
by: Razzie | last post by:
Hi all, A question from someone on a website got me thinking about this, and I wondered if anyone could explain this. A System.Threading.Timer object is garbage collected if it has no...
16
by: droopytoon | last post by:
Hi, I start a new thread (previous one was "thread timing") because I have isolated my problem. It has nothing to do with calling unmanaged C++ code (I removed it in a test application). I...
9
by: mareal | last post by:
I have noticed how the thread I created just stops running. I have added several exceptions to the thread System.Threading.SynchronizationLockException System.Threading.ThreadAbortException...
13
by: Bob Day | last post by:
Using vs2003, vb.net I start a thread, giving it a name before start. Code snippet: 'give each thread a unique name (for later identification) Trunk_Thread.Name = "Trunk_0_Thread" ' allow...
20
by: Bob Day | last post by:
Using VS 2003, VB, MSDE... There are two threads, A & B, that continously run and are started by Sub Main. They instantiationsl of identical code. Thread A handles call activity on telephone...
3
by: John Nagle | last post by:
There's no way to set thread priorities within Python, is there? We have some threads that go compute-bound, and would like to reduce their priority slightly so the other operations, like...
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: 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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...

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.