473,698 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Thread and GUI update problem

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 Thread is then created to load the bulk data
3) after the thread has completed, close the "Wait for loading" form
4) show the main form for the GUI program

The problem is that if I show the "waiting" form, that form's GUI will
not work properly (no repaint event and hangs around, ie. a blank
window), the worse thing is that the Thread may not work properly too
(actually it will stop executing, waiting for locks, maybe ??)

But if I start the Thread without showing any forms; it works
perfectly ????!

What's wrong ?? (the same things happen under .Net Compact Framework)

[code snippet for the main GUI form]
public class Trial04_02 : System.Windows. Forms.Form
{
private System.Windows. Forms.Label label1;
private System.Windows. Forms.Button button1;
private System.Componen tModel.Containe r components = null;
private Trial04_01 mFrmSplash;
private Thread mThr_Main;
private System.Windows. Forms.ListBox listBox1;
private CommonEngine02 mCEng;

public Trial04_02()
{
InitializeCompo nent();
init ();
}

private void InitializeCompo nent() {}

protected void init ()
{
// 1st show splash form
this.mFrmSplash = new Trial04_01 ();
//this.mFrmSplash .Show ();

// 2nd create a thread to load sth...
// CommonEngine02 is a class containing the
// data loading function
this.mCEng = new CommonEngine02 (); this.mThr_Main = new Thread
(new ThreadStart (this.mCEng.thr eadTask));
this.mThr_Main. Start ();
this.mThr_Main. Join ();
this.mFrmSplash .Close ();

// 3rd other setup(s)
// get back the loaded data
System.Collecti ons.ArrayList oArr
= this.mCEng.getA rr_Data ();
for (int i=0; i<oArr.Count; i++)
{
this.listBox1.I tems.Add (oArr[i]);
}
this.Show ();
}

static void Main ()
{
Application.Run (new Trial04_02 ());
}
}
}

[/code]
From Jason (Kusanagihk)
Nov 16 '05 #1
13 3623
Hi Jason,

It looks like your form "mFrmSplash " (main thread) waits at
"this.mThr_Main .Join()" line, so it can not be "invalidate d".

I can propose:
A) "mFrmSplash.Clo se() && this.Show()" within
thread "mThr_Main" .
or
B) run third thread that will wait ("this.mThr_Mai n.Join()")
and then will close the splash form and show the main window.

Regads

Marcin
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 Thread is then created to load the bulk data
3) after the thread has completed, close the "Wait for loading" form
4) show the main form for the GUI program

The problem is that if I show the "waiting" form, that form's GUI will
not work properly (no repaint event and hangs around, ie. a blank
window), the worse thing is that the Thread may not work properly too
(actually it will stop executing, waiting for locks, maybe ??)

But if I start the Thread without showing any forms; it works
perfectly ????!

What's wrong ?? (the same things happen under .Net Compact Framework)

[code snippet for the main GUI form]
public class Trial04_02 : System.Windows. Forms.Form
{
private System.Windows. Forms.Label label1;
private System.Windows. Forms.Button button1;
private System.Componen tModel.Containe r components = null;
private Trial04_01 mFrmSplash;
private Thread mThr_Main;
private System.Windows. Forms.ListBox listBox1;
private CommonEngine02 mCEng;

public Trial04_02()
{
InitializeCompo nent();
init ();
}

private void InitializeCompo nent() {}

protected void init ()
{
// 1st show splash form
this.mFrmSplash = new Trial04_01 ();
//this.mFrmSplash .Show ();

// 2nd create a thread to load sth...
// CommonEngine02 is a class containing the
// data loading function
this.mCEng = new CommonEngine02 (); this.mThr_Main = new Thread
(new ThreadStart (this.mCEng.thr eadTask));
this.mThr_Main. Start ();
this.mThr_Main. Join ();
this.mFrmSplash .Close ();

// 3rd other setup(s)
// get back the loaded data
System.Collecti ons.ArrayList oArr
= this.mCEng.getA rr_Data ();
for (int i=0; i<oArr.Count; i++)
{
this.listBox1.I tems.Add (oArr[i]);
}
this.Show ();
}

static void Main ()
{
Application.Run (new Trial04_02 ());
}
}
}

[/code]
From Jason (Kusanagihk)

Nov 16 '05 #2
Jason Jacob <50******@alumn i.cityu.edu.hk> wrote:
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 Thread is then created to load the bulk data
3) after the thread has completed, close the "Wait for loading" form
4) show the main form for the GUI program


There's no point in creating a new thread if you're immediately going
to call Join on it. Instead, you need to run the UI thread as normal,
and have some kind of call when the load finishes to tell the loading
screen to close.

See http://www.pobox.com/~skeet/csharp/multithreading.html for more
information.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
To Jon,

I've changed the code as you've said, actually there is no change in
the problem:

public Form1()
{
InitializeCompo nent();

oFrm2 = new Form2 ();
Thread t1 = new Thread
(new ThreadStart (oFrm2.threadTa sk));
t1.Start ();

loaderTask ();
oFrm2.mBool_Sto p = true;
lock (this)
{
t1.Join ();
oFrm2.Close ();
}
}

The above code is the constructor code for the operation, threadTask
will change the Splash Form's Label's Text, but when got them
executed, the Splash Form's UI is not updating properly...

But then, I've tried to use oFrm2.ShowDialo g () instead of oFrm2.Show
(), the result is that the Splash Form can be shown properly, but
actually it is not a Thread, since the form is waiting for you to
close it (Dialog), then it will continue the other tasks started
earlier...

From Jason (Kusanagihk)
Nov 16 '05 #4
Jason Jacob <50******@alumn i.cityu.edu.hk> wrote:
I've changed the code as you've said, actually there is no change in
the problem:

public Form1()
{
InitializeCompo nent();

oFrm2 = new Form2 ();
Thread t1 = new Thread
(new ThreadStart (oFrm2.threadTa sk));
t1.Start ();

loaderTask ();
oFrm2.mBool_Sto p = true;
lock (this)
{
t1.Join ();
oFrm2.Close ();
}
}

The above code is the constructor code for the operation, threadTask
will change the Splash Form's Label's Text, but when got them
executed, the Splash Form's UI is not updating properly...
The constructor is not going to finish executing until the thread has
finished though, so you aren't starting a message pump.
But then, I've tried to use oFrm2.ShowDialo g () instead of oFrm2.Show
(), the result is that the Splash Form can be shown properly, but
actually it is not a Thread, since the form is waiting for you to
close it (Dialog), then it will continue the other tasks started
earlier...


In the above, you aren't even showing oFrm2 at all. You should start
the thread and then have that tell the splash screen to close when it's
finished. Using Thread.Join is *not* a good idea when called from the
UI thread.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
To Jon,

I've modified the code, and start the Thread outside the constructor.
Now it is works a bit "normal" to me. However the Splash Form still
can't change its GUI's properties (eg. I want to change a Label's text
showing the Time elapsed from loading etc..., it still won't update
for now)

And Join a UI Thread is not a *good* idea, what does that mean ??

the new code
Expand|Select|Wrap|Line Numbers
  1. namespace Splash_Trial.Trial02
  2. {
  3. public class Form1 : System.Windows.Forms.Form
  4. {
  5. ..........
  6.  
  7. // Constructor
  8. public Form1()
  9. {
  10. InitializeComponent();
  11.  
  12. this.Show ();
  13. }
  14.  
  15. protected void loaderTask ()
  16. {
  17. // do some bulky things such as reading lots of data
  18. ...............
  19. }
  20.  
  21. protected override void Dispose( bool disposing ){ //.... }
  22.  
  23. protected void threadTask ()
  24. {
  25. oFrm2 = new Form2 ();
  26. Thread t1 = new Thread
  27. (new ThreadStart (oFrm2.threadTask));
  28. t1.Start ();
  29.  
  30. loaderTask ();  // the bulky work
  31. oFrm2.mBool_Stop = true;
  32. t1.Join ();
  33. oFrm2.Close ();
  34. }
  35.  
  36. [STAThread]
  37. static void Main()
  38. {
  39. Form1 oFrm1 = new Form1 ();
  40. oFrm1.threadTask ();
  41. Application.Run(oFrm1);
  42. }
  43. }
  44. }
  45.  
  46.  
From Jason (Kusanagihk)
Nov 16 '05 #6
Jason Jacob <50******@alumn i.cityu.edu.hk> wrote:
I've modified the code, and start the Thread outside the constructor.
Now it is works a bit "normal" to me. However the Splash Form still
can't change its GUI's properties (eg. I want to change a Label's text
showing the Time elapsed from loading etc..., it still won't update
for now)
You need to use Control.Invoke or Control.BeginIn voke to update the UI
from a non-UI thread. Did you read the article I pointed you to before?
It has an example of doing this kind of thing.
And Join a UI Thread is not a *good* idea, what does that mean ??


It means you shouldn't do it, because your app's UI won't be able to
process any events (like painting) until the other thread has finished.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
To Jon

I've read the "Threading in Windows Form" from your link. I have
modified the code to use Control.Invoke, but still some strange
things. If I don't add a Refresh () code, the UI won't update; if I
try to click or do anything on the splash form, the UI will not
respond (a "not responding" appears on the form's title bar.

using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;

using System.Threadin g;

namespace ThreadCookBook. Trial05
{
public class Form2 : System.Windows. Forms.Form
{
private System.Windows. Forms.Label label1;
private System.Componen tModel.Containe r components = null;
public delegate void invokeTask ();
public invokeTask mInvokeTask;
private bool mBool_Stop = false;

public Form2()
{
InitializeCompo nent();
this.mInvokeTas k = new invokeTask
(this.realInvok eTask);
}

public void realInvokeTask ()
{
Random ran = new Random
((int) System.DateTime .Now.Ticks);
int iR = ran.Next (255);
int iG = ran.Next (255);
int iB = ran.Next (255);

this.label1.Tex t = System.DateTime .Now + "";
this.BackColor = Color.FromArgb (iR, iG, iB);
this.Refresh ();
}

protected override void Dispose( bool disposing ) { //... }
private void InitializeCompo nent() { //... }

public void threadTask ()
{
if (this.Visible == false)
{
this.Show ();
}
while (!this.mBool_St op)
{
Invoke (this.mInvokeTa sk);
Thread.Sleep (2000);
}
}

public void setStop (bool flag)
{
this.mBool_Stop = flag;
}
}
}
From Jason (Kusanagihk)
Nov 16 '05 #8
Jason Jacob <50******@alumn i.cityu.edu.hk> wrote:
I've read the "Threading in Windows Form" from your link. I have
modified the code to use Control.Invoke, but still some strange
things. If I don't add a Refresh () code, the UI won't update; if I
try to click or do anything on the splash form, the UI will not
respond (a "not responding" appears on the form's title bar.


Well you're now not showing the code which creates the new thread.
Please post a *complete* program which demonstrates the problem, and
we'll be able to get somewhere.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9
To Jon

The Main Form's code

using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Threadin g;

namespace ThreadCookBook. Trial05
{
public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.ListBox listBox1;
private System.Windows. Forms.Button button1;
private System.Componen tModel.Containe r components = null;
private Thread mThr_ThreadTask ;
// a class to load some bulky data
private CommonEngine02 mCEng02;
// the Splash Form
private Form2 mFrm02;

public Form1()
{
InitializeCompo nent();
init ();
}

protected override void Dispose( bool disposing ) { //... }
private void InitializeCompo nent() { //... }

protected void init ()
{
this.mCEng02 = new CommonEngine02 ();
}

protected void loaderTask ()
{
this.threadTask ();
// blocking, load the bulky data ...
this.mCEng02.th readTask ();
// try to stop the loop in the Splash Form
lock (this)
{
this.mFrm02.set Stop (true);
this.mFrm02.Clo se ();
}
// get back the bulky data and add them to a listBox
ArrayList oArr = this.mCEng02.ge tArr_Data ();
for (int i=0; i<oArr.Count; i++)
{
this.listBox1.I tems.Add (oArr[i]);
}
}

public void threadTask ()
{
this.mFrm02 = new Form2 ();
this.mThr_Threa dTask = new Thread
(new ThreadStart (this.mFrm02.th readTask));
this.mThr_Threa dTask.Start ();
}

static void Main ()
{
// maybe here's where the problem is...
Form1 oFrm01 = new Form1 ();
oFrm01.loaderTa sk ();
Application.Run (oFrm01);
}
}
}

From Jason (Kusanagihk)
Nov 16 '05 #10

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

Similar topics

44
2362
by: Charles Law | last post by:
Hi guys. I'm back on the threading gig again. It's the age-old question about waiting for something to happen without wasting time doing it. Take two threads: the main thread and a worker thread. The worker thread is reading the serial port, waiting for something to happen (a service request). When it does it raises an event. Of course, the event is executed on the worker thread. The idea is that when the event is raised, the handler...
12
1548
by: serge calderara | last post by:
Dear all, I have a function that I need to run in a thread due to the fact that it can takes long time to execute according to the amount of data to collect. This function is also populating a treeview control whne collected data gets finished. In order to achieve this I have used the following code : System.Threading.ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf GetListOfReelsFromRemoteDB))
2
11785
by: BG | last post by:
We're having trouble writing the code to update a UI control (label.Text) from a secondary thread. We're using C# with Windows Forms. We have a main form named MainForm, a splash screen form named SplashScreen, and a C# class library named BackgroundProcess.
2
1744
by: Don Tucker | last post by:
Hello, I am using Visual Studio 2005 .Net, coding in C#. I am working through the threading walkthrough: ms-help://MS.VSCC.v80/MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_fxmclicc/html/7bc03b7b-d680-499b-8179-5f414b2d650c.htm and have been able to get that to work as designed. However, I coded up a slight variant of that example to update the GUI while each thread is running rather than only at the end of each thread. My...
9
5823
by: Jervin Justin | last post by:
Hi, I've been having this problem for some time with Web Forms: I have a web app that sends data to a service when the user presses a button. Based on the data, the server will send several replies. Since there is no way for a service to post to a client, I am using a thread on the client that polls the server for updates. (Feel free to recommend changes to that as well)
4
3574
by: Charles Law | last post by:
Hi guys. I have two threads: a main thread and a background thread. Lots of stuff happens in the background thread that means I have to update several (lots) of controls on a form. It is quite tiresome to have to write code to call MyControl.Invoke for each control on the form, along with the delegates that are required for each. Is there a better way to do this? What I mean is, if I could marshal the
14
6878
by: joey.powell | last post by:
I am using VS2005 for a windows forms application. I need to be able to use a worker thread function to offload some processing from the UI thread. The worker thread will need access to a datagridview on the form. I am using the following code to spawn the worker thread... Thread WorkerThread = new Thread(new ThreadStart(WT_MyFunction)); WorkerThread.IsBackground = true; WorkerThread.Start(); The problem I am having is...I cannot seem...
8
5362
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 this code there is a panel panel1, that I populate with a lable in the foreground. Then when I click on "button1" a backgroundworker thread in async mode is started. When the backgoundworker thread completes the thread returns a panel to populate...
20
5530
by: cty0000 | last post by:
I have some question.. This is my first time to use thread.. Following code does not have error but two warring The warring is Warning 2 'System.Threading.Thread.Suspend()' is obsolete: 'Thread.Suspend has been deprecated. Please use other classes in System.Threading, such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect resources. http://go.microsoft.com/fwlink/?linkid=14202'
5
1787
by: P.J.M. Beker | last post by:
Hi there, I'm currently writing a program in which I use the FileMonitor to monitor a folder in which I store downloaded images. I know that I can't add much coding in the filemonitor's event in risk of losing some new entries, so I've deceided to create an update thread. This thread is created when the program's start and should (for various reason) run not in sync with the Filemonitor. The Filemonitor event creates an entry in a...
0
8674
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
9157
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
9023
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6518
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
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
4366
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
4615
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2327
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.