473,396 Members | 2,010 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,396 software developers and data experts.

help explain this winform behaviour

Hi, everyone. I ran into an UI freeze issue with threading and
simplied the problem down to the following sample code. The logic is
as follows:

1. The main UI thread creates a non-UI STA thread(m_MainThread).
2. Then the non-UI thread runs and creates derived winform and holds
a reference to the winform. NOTE: I don't call Application.Run()
because I don't need a message pump on this "hidden form" in my
application. I rely on the form's STA thread owner to pump and
dispatch non-COM messages to the form window on a WaitOne() call.
This works fine. I can send and receive messages to external win32
process using the hidden form.

The code below works fine in .NET 1.1, but causes UI freeze issue
in .NET 2.0 outside of the debugger. To produce the freeze behaviour,
I do the following:

1. Run the code below
2. Click on the Start button to create the non-UI thread(which
creates the hidden form)
3. right click on Desktop -Properties -Settings -change your
screen resolution to something less and click OK
4. When asked if you want to keep the setting, click NO to go back.

At this point, you will notice that the main form is now
frozen(verified with spy++ that it doesn't get any messages). It
looks like it's deactivated somehow. You can't move the window and
the form won't paint properly if you overlap it with another window.

The same code below doesn't have the problem in .NET 1.1. Also, the
problem doesn't occur in .NET 2.0 if you run it under the debugger.

If I don't create the hidden winform, there is no problem.

Can someone explain what's happening? Thanks.
================= Code Starts Here =================

public partial class Form1 : Form
{
AutoResetEvent m_MainThreadStopEvent = new
AutoResetEvent(false);
Thread m_MainThread = null;
HiddenForm m_WorkerHiddenForm = null;

public Form1()
{
InitializeComponent();
}

private void Start()
{
m_MainThread = new Thread(new
ThreadStart(MainThreadStart));
m_MainThread.Name = "My Main Thread";
m_MainThread.SetApartmentState(ApartmentState.STA) ;
m_MainThread.Start();
}

private void Stop()
{
m_MainThreadStopEvent.Set();
}

private void MainThreadStart()
{
System.Diagnostics.Debug.WriteLine("Created Main
Thread ... " + DateTime.Now.ToLongTimeString());
m_WorkerHiddenForm = new HiddenForm();

while (!m_MainThreadStopEvent.WaitOne(5000, true))
{
System.Diagnostics.Debug.WriteLine("::MainThread ... "
+ DateTime.Now.ToLongTimeString());
}
m_WorkerHiddenForm.Close();

System.Diagnostics.Debug.WriteLine("Stopped Main
Thread ... " + DateTime.Now.ToLongTimeString());
}

private void btnStart_Click(object sender, EventArgs e)
{
Start();
}

private void btnStop_Click(object sender, EventArgs e)
{
Stop();
}

private void Form1_FormClosing(object sender,
FormClosingEventArgs e)
{
Stop();
}
}
public class HiddenForm : Form
{
int m_WindowHandle;
public HiddenForm()
{
m_WindowHandle = this.Handle.ToInt32();
}

protected override void WndProc(ref Message msg)
{
base.WndProc(ref msg);
}
}

Feb 3 '07 #1
0 1998

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

Similar topics

0
by: someone | last post by:
Hi, We are encountering a problem with the DTPicker. This is the situation: We have a DTPicker-control on a VB6 Active-X. The Active-X is used on a .NET winform. the problem: The .Net...
0
by: Brian | last post by:
Hello, I usually login a forum and post some messages. I can't directly post message because the forum ask me to login first. I guess it need some session information. Now I want to program a...
2
by: Stan Sainte-Rose | last post by:
Hi, Newbie I am, I need to understand something with vb. I have a winform (A) where the user fills out some datas. On this form, I have a combo box where he can select an item. If the item...
5
by: Alien2_51 | last post by:
I have a problem with a ListBox control that is on a TabControl, it seems to be forgetting which items are selected in the list when I tab off the current tab, here's my winform code... I even...
7
by: Stan | last post by:
I need to attach to aspnet_wp.exe in order to debug a web serivice from WinForm client. Why don't I have to do this for ASP.NET / WebForm client? Why doesn't VS.NET debugger attaches...
6
by: Annie | last post by:
hello guys, I have a number of checkbox contorls all together (I don't want option buttons - users request) ... I just want the users be able to select only one of the checkboxes ... if a...
5
by: Galen Somerville | last post by:
Tooo many examples. I'm confused. VB6 ActiveX.dll with no windows or controls. Interfaces with a device that returns an x size byte array every x milliseconds for x counts. Sends a message to...
11
by: jjbutera | last post by:
I know how to use the ErrorProvider in my winforms..or do I? I validate the values and set the ErrorProvider in the validating event. If not valid, I set e.Cancel = True. I clear the ErrorProvider...
12
by: zou | last post by:
there is a file which is very large, we can use stat to get a file size(<2G), struct stat buf; stat("file", &buf); long s=(long)stat.st_size; but stat::st_size is type of off_t(typedef...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.