473,788 Members | 2,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Winforms, Shown Event and SetEnvironmentV ariable


I am running into a weird problem in my ultra-simple Winforms
application written in C#.

In the Form.Shown event I set a couple of environment variables using
the standard SetEnvironmentV ariable API like so:

Environment.Set EnvironmentVari able("someKey", somevalue,
EnvironmentVari ableTarget.User );

I have 2 such calls. Amazingly it takes nearly 10 seconds for these
two calls to execute! If I comment out these 2 calls and run, the
application is just spiffy and the dialog comes up in no time.
Because it takes 10 seconds for these calls to execute, the dialog
kinda hangs before becoming responsive for that duration of time
(obviously because the UI thread is held up).

Am I doing something wrong?

Feb 14 '08 #1
8 2809
On Feb 14, 3:50 pm, Dilip <rdil...@lycos. comwrote:
I am running into a weird problem in my ultra-simple Winforms
application written in C#.

In the Form.Shown event I set a couple of environment variables using
the standard SetEnvironmentV ariable API like so:

Environment.Set EnvironmentVari able("someKey", somevalue,
EnvironmentVari ableTarget.User );

I have 2 such calls. Amazingly it takes nearly 10 seconds for these
two calls to execute! If I comment out these 2 calls and run, the
application is just spiffy and the dialog comes up in no time.
Because it takes 10 seconds for these calls to execute, the dialog
kinda hangs before becoming responsive for that duration of time
(obviously because the UI thread is held up).

Am I doing something wrong?
Apologies.. false alarm!
It was a different problem.
Feb 14 '08 #2
On Thu, 14 Feb 2008 14:06:28 -0800, Dilip <rd*****@lycos. comwrote:
Apologies.. false alarm!
It was a different problem.
What was it? As I mentioned, I was able to easily reproduce what you
described. Are you saying that you're not actually having that problem?

Pete
Feb 14 '08 #3
On Feb 14, 4:21 pm, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
wrote:
On Thu, 14 Feb 2008 14:06:28 -0800, Dilip <rdil...@lycos. comwrote:
Apologies.. false alarm!
It was a different problem.

What was it? As I mentioned, I was able to easily reproduce what you
described. Are you saying that you're not actually having that problem?

Pete
Peter
I am sorry once again. Its now a false alarm on top of a false
alarm :-)
My original post was accurate. Those two calls to the
SetEnvironmentV ariable does take nearly 10 seconds to execute (I timed
it with System.Diagnost ics.Stopwatch).

This is weird because I earlier had these calls in a console app and I
never saw this delay. Putting them in a Winforms app however
introduces it. I can't understand why.

I am going to run CLR profiler and see what is bottlenecking it. I
haven't read your other post yet.
Will get to it after I post this.
Feb 15 '08 #4
On Sat, 16 Feb 2008 09:24:45 -0800, Willy Denoyette [MVP]
<wi************ *@telenet.bewro te:
I can't repro this using a minimal Forms applications that sets two
environment variables in the Shown handler.
The SetEnvironmentV ariable calls take less than 2 millisecond. to
complete.
Are you running in a local account, or are you running in a domain
account?
What version of the framework and OS are you running?
I was able to reproduce the behavior easily. So, from my configuration:

Windows XP SP2, IE7 (it shouldn't matter, but as we know it sometimes
does), all critical updates installed (including this month's release).
..NET 2.0 installed (SP1, if I recall correctly). User is a
restricted/limited user, local account, and the target for the call to
SetEnvironmentV ariable is EnvironmentVari ableTarget.User .

As I noted in a different message, running the code in the GUI thread
results in a 10 second delay (I didn't measure the two calls
independently, so I don't know if it's a single 10 second delay, or two 5
second delays). Running the same exact code in a thread pool thread (via
BackgroundWorke r) results in a much smaller delay (700-900ms...still
longer than I'd expected, but much more acceptable).

Are you sure you're using the EnvironmentVari ableTarget.User as the target
in your tests?

Pete
Feb 16 '08 #5
"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Sat, 16 Feb 2008 09:24:45 -0800, Willy Denoyette [MVP]
<wi************ *@telenet.bewro te:
>I can't repro this using a minimal Forms applications that sets two
environment variables in the Shown handler.
The SetEnvironmentV ariable calls take less than 2 millisecond. to
complete.
Are you running in a local account, or are you running in a domain
account?
What version of the framework and OS are you running?

I was able to reproduce the behavior easily. So, from my configuration:

Windows XP SP2, IE7 (it shouldn't matter, but as we know it sometimes
does), all critical updates installed (including this month's release).
.NET 2.0 installed (SP1, if I recall correctly). User is a
restricted/limited user, local account, and the target for the call to
SetEnvironmentV ariable is EnvironmentVari ableTarget.User .

As I noted in a different message, running the code in the GUI thread
results in a 10 second delay (I didn't measure the two calls
independently, so I don't know if it's a single 10 second delay, or two 5
second delays). Running the same exact code in a thread pool thread (via
BackgroundWorke r) results in a much smaller delay (700-900ms...still
longer than I'd expected, but much more acceptable).

Are you sure you're using the EnvironmentVari ableTarget.User as the target
in your tests?
Yep, these two:
Environment.Set EnvironmentVari able("someKey", "1",
EnvironmentVari ableTarget.User );
Environment.Set EnvironmentVari able("someOther Key", "2",
EnvironmentVari ableTarget.User );

in Shown handler.

Running V2 SP1 of the FW on VISTA.
Timing done using "QueryThreadCyc leTime" API (Vista and up), with Processor
clock at fixed rate.
Total time for the two consecutive calls: ~3.6 msec. on 2.2Ghz Dual core
AMD64
~3 msec. on 3GHz Intel Core Duo.

Willy.
Feb 16 '08 #6
On Sat, 16 Feb 2008 13:42:49 -0800, Willy Denoyette [MVP]
<wi************ *@telenet.bewro te:
Yep, these two:
Environment.Set EnvironmentVari able("someKey", "1",
EnvironmentVari ableTarget.User );
Environment.Set EnvironmentVari able("someOther Key", "2",
EnvironmentVari ableTarget.User );

in Shown handler.

Running V2 SP1 of the FW on VISTA.
Timing done using "QueryThreadCyc leTime" API (Vista and up), with
Processor clock at fixed rate.
Why are you doing that? The question here is elapsed time, not the CPU
time consumed by the thread for the calls.

If you just put a Stopwatch in the Shown event handler, start it before
the two calls, and check the Elapsed time after them, what results do you
get?

Not that it's relevant to this question IMHO, but what's the .NET API for
the QueryThreadCycl eTime call (if any)? I can only find the unmanaged
version. Is there a managed way to get at that? Maybe in WMI?

Pete
Feb 17 '08 #7
"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Sat, 16 Feb 2008 13:42:49 -0800, Willy Denoyette [MVP]
<wi************ *@telenet.bewro te:
>Yep, these two:
Environment.Se tEnvironmentVar iable("someKey" , "1",
EnvironmentVar iableTarget.Use r);
Environment.Se tEnvironmentVar iable("someOthe rKey", "2",
EnvironmentVar iableTarget.Use r);

in Shown handler.

Running V2 SP1 of the FW on VISTA.
Timing done using "QueryThreadCyc leTime" API (Vista and up), with
Processor clock at fixed rate.

Why are you doing that? The question here is elapsed time, not the CPU
time consumed by the thread for the calls.
One reason is higher precision, initially I used Stopwatch and soon found
out that the elapsed time was way below 1 second (see later), also, I don't
like to use Stopwatch for anything that takes less than a second.
The other reason is that I wanted to measure the time taken by the
*current* UI thread to perform these two API calls, not the time taken by
the other threads in the system. The reason is that SetEnvironmentV ariable
calls Win32's "SendMessageTim eout" to broadcast a message to all top level
windows (except the owning window) and does not return before the message
has been processed by all *other* top level windows or after a 1000 msec.
time-out period (per window). When the system broadcasts a message to a
top-level window who's thread doesn't pump it's message queue, it will wait
a max of 5 seconds before returning, which looks like what's happening in
your (and the OP's) case, though I don't understand why it's working from a
console app.
If you just put a Stopwatch in the Shown event handler, start it before
the two calls, and check the Elapsed time after them, what results do you
get?
~90-100 msecs. using ...
double elapsed = ((double)sw.Ela psedTicks/swFrequency);
with ~10 top-level windows.
This is considerably longer merely because of the extra thread switches
Not that it's relevant to this question IMHO, but what's the .NET API for
the QueryThreadCycl eTime call (if any)? I can only find the unmanaged
version. Is there a managed way to get at that? Maybe in WMI?
QueryThreadCycl eTime, QueryProcessCyc leTime, QueryIdleProces sorCycleTime and
a couple of others are new Win32 API's in Vista and up.
..NET lags behind here, so the only way to use these high precision low
latency counters is by PInvoke.
WMI does/can't expose this, but the VS profiler can be configured to use
these counters, also ETW uses the high precision counters on Vista and
WS2008.

Willy.
Feb 17 '08 #8
"Willy Denoyette [MVP]" <wi************ *@telenet.bewro te in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
"Peter Duniho" <Np*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
>On Sat, 16 Feb 2008 13:42:49 -0800, Willy Denoyette [MVP]
<wi*********** **@telenet.bewr ote:
>>Yep, these two:
Environment.S etEnvironmentVa riable("someKey ", "1",
EnvironmentVa riableTarget.Us er);
Environment.S etEnvironmentVa riable("someOth erKey", "2",
EnvironmentVa riableTarget.Us er);

in Shown handler.

Running V2 SP1 of the FW on VISTA.
Timing done using "QueryThreadCyc leTime" API (Vista and up), with
Processor clock at fixed rate.

Why are you doing that? The question here is elapsed time, not the CPU
time consumed by the thread for the calls.
One reason is higher precision, initially I used Stopwatch and soon found
out that the elapsed time was way below 1 second (see later), also, I
don't like to use Stopwatch for anything that takes less than a second.
The other reason is that I wanted to measure the time taken by the
*current* UI thread to perform these two API calls, not the time taken by
the other threads in the system. The reason is that SetEnvironmentV ariable
calls Win32's "SendMessageTim eout" to broadcast a message to all top level
windows (except the owning window) and does not return before the message
has been processed by all *other* top level windows or after a 1000 msec.
time-out period (per window). When the system broadcasts a message to a
top-level window who's thread doesn't pump it's message queue, it will
wait a max of 5 seconds before returning, which looks like what's
happening in your (and the OP's) case, though I don't understand why it's
working from a console app.
>If you just put a Stopwatch in the Shown event handler, start it before
the two calls, and check the Elapsed time after them, what results do you
get?
~90-100 msecs. using ...
double elapsed = ((double)sw.Ela psedTicks/swFrequency);
with ~10 top-level windows.
This is considerably longer merely because of the extra thread switches
>Not that it's relevant to this question IMHO, but what's the .NET API for
the QueryThreadCycl eTime call (if any)? I can only find the unmanaged
version. Is there a managed way to get at that? Maybe in WMI?
QueryThreadCycl eTime, QueryProcessCyc leTime, QueryIdleProces sorCycleTime
and a couple of others are new Win32 API's in Vista and up.
.NET lags behind here, so the only way to use these high precision low
latency counters is by PInvoke.
WMI does/can't expose this, but the VS profiler can be configured to use
these counters, also ETW uses the high precision counters on Vista and
WS2008.

Willy.



Please note that I don't have handlers for WM_SETTINGCHANG E in the other
running windows applications, other than the default handler.

Willy.

Feb 17 '08 #9

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

Similar topics

10
2784
by: J.Marsch | last post by:
I know that the controls on a Winform are not thread safe, and that you want to do all of your UI updates on a single thread -- generally the main thread. Now, 2 questions: 1. Does the one thread that you can use to update controls have to be the thread that you were on when you instanced the form? 2. Can 2 forms in the same application have affinity for different threads, or do you want all of the forms in an application to be...
2
7368
by: frank_ulbricht | last post by:
Hi folks, I'm a Java programmer and now I have to use C# for a new large project. The language is the best I've ever seen. In some points it goes far beyond Java. I like that. But then there was a great dissappointments about WinForms. Compared with Java/Swing (skip the performance, I mean the development) it is very poor at first sight. It reminds me when I startet in 1995 with Delphi (guess a guy named Heilsberg is the reason ...
0
1385
by: Shamil Salakhetdinov | last post by:
Hi All, Have anybody seen somewhere techniques like the one described here: Automating Windows Applications http://www.thecodeproject.com/com/automatingwindowsapps.asp but for managed WinForms application? Basically what is needed:
10
5518
by: denis.greatest | last post by:
Hello folks, I was trying to set some environment variable for a process I create. (The variables are my own, not system). I use SetEnvironmentVaraible() from C# code. In Debug configuration this works fine, but in Release configuration the action fails. The return value is TRUE, and no Exception is thrown.
2
2021
by: Kurt Denhoff | last post by:
I have an odbc connection on a Winforms UserControl when used within a WinForms project it works fine but when I use it on an aspx page the object doesn't show. The IIS log indicates code 200(successfully loaded). I am suspecting this may be an issue with being fully trusted dll. I experimented with the ODBC connection on a Webform control - it worked fine. I put the ODBC connection on the aspx page - it worked fine. I put the...
0
2492
by: jonathan.beckett | last post by:
I have been working on a client project recently that is using winforms ..NET user controls within web pages in Internet Explorer, and need to find out how to make the user control communicate back to the webpage (typically to tell it that it has finished doing something). I started digging around and found out that I needed to implement a COM interface for the .NET user control, and pick up the events from that in Javascript on the...
2
7030
by: CSGeach | last post by:
Hello all In VB6, if you referred to a property or a some property of a control of an unloaded form, it caused a form_load, and fired the Form_Load event. I have a lot of code that depends on this behaviour: thus in order to achieve some initialization of my form , I have a general purpose routine as follows Public Function ShowForm(ByRef FormToShow As Object, ByRef ShowModal As Boolean, ByRef FormTag As String) As Boolean '...
4
5988
by: parez | last post by:
Hi, Whats the winforms equivalent of asp.net page load event? I am trying to clear a status message field every time a user clicks on any of the buttons..
23
4561
by: raylopez99 | last post by:
Here I am learning WinForms and two months into it I learn there's a WPF API that is coming out. Is this WPF out yet, and is it a threat to WinForms, in the sense that all the library routines I memorize and/ or familiarize myself in WinForms will disappear in WPF? I did note in one early version of C#, the non-generic "ArrayList" was replaced by the generic and template <based List, but that kind of change is not a big deal. If WPF...
0
9498
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
10364
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
10172
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
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
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
6750
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
5398
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
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
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

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.