Yesterday, I posted a message here about creating a modeless form with a single label control that was not being rendered by the form. Towards the end of the work day yesterday, we had all thought it might just be a problem on my computer (VS2005/SP1 on Vista). However, when I brought the code home and tried it, it happened there too (VS2005/SP1 on XP). It also happened on VS2008 using .Net 2.0 and then using .Net 3.5.
What's really disturbing is that two people here on CP tried the same code, and it did not occur for them.
This morning, I figured out how to force it to render the control. I added this function to the modeless form:
public void UpdateForm()
and then modified the code that opens/closes the form (remember, this code is in the DoWork() handler of a BackgroundWorker object) looks like this:
private void splashWorker_DoWork(object sender,DoWorkEventArgs e)
BackgroundWorker thisWorker = sender as BackgroundWorker;
SplashPanel splashPanel = new SplashPanel();
I couldn't find ANYBODY ELSE on the net that had experienced the non-rendered controls problem that I've experienced. In the process of trying to figure t=out the problem, I tried using calls to MessageBox.Show() within various form event handlers, and discovered that the act of displaying a message box caused the label control to be properly displayed. Invalidating the control didn't have any effect. Hiding/showing the control didn't work. It was only out of desperation that I finally discovered that calling BringToFront() would show the control.
I want to know WHY this is necessary, or what is wrong with my Visual Studio (or .Net) install that would cause this problem.
[EDIT] I've come to the conclusion that this is a bug in the .Net framework, starting all the way back at .Net 1.0, and is probably related to the Event loop not being fired correctly. I added a bunch of other types of controls and all of them exhibited the same behavior as my original label control. Everything is fine as long as you are using a modal form (using ShowDialog() to display the form). However, displaying the form as modeless, you get screwed up up results.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
Is it possible to change the accessiblity of a property in code?
Why? I have a multi-function textbox class which has MinLength, MaxLength and RestrictionType (enum) properties.
1. If RestrictionType == Restriction.None the MinLength and MaxLength properties are read/write.
2. If RestrictionType == Restriction.IMEI I set the MinLength and MaxLength properties to 15. At this point I'd like to make MinLength and MaxLength read only as they shouldn't be changed while this RestricionType is active.
Obviously, at the moment I check the RestrictionType in the setters and reject the change if RestrictionType == Restriction.IMEI but I wondered if another way was possible.
Dave BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
I have created a dll in c# for crawling the documents from sharepoint and to get the modified date,created Date..etc..
When i run it from server where sharepoint is installed its getting the created date and modified Date....
But when i run it from client pc...It is giving Debug Error....
when i comment this code....which i used for getting created date and modified date...
the code is
using (SPSite RootSite = new SPSite(listPath))
Console.WriteLine("1Entered SPSIte path");
using (SPWeb SiteCollection = RootSite.OpenWeb())
string path = listPath;// RootSite.MakeFullUrl(PageName);
SPFile file = SiteCollection.GetFile(path);
string TimeCreated = file.TimeCreated.ToLongDateString();
Console.WriteLine("1.1: Created Date is :" + TimeCreated);
when i comment this code it works fine....is there any problem in it...but the same code works fine in server pc...I am totally confused..please give some idea......I am totally new to sharepoint and c#
I am Mnaju.I have Completed my B.E Computers Science.Lokking for a job.I am interested in VC++
I'm writting an application that makes several instances of Internet Explorer object to render webs and record the content. To make the video it's very easy 'cause I can take snapshots of the web rendering, but I don't know how I can record the sound of different sites simultaneously.
Fields that are declared volatile are not subject to compiler optimizations that assume access by a single thread
This is from the VS help about the volatile keyword in C#.
Does that mean normal variables are cached and to optimize speed, the cached value is used unless the variable is changed in the current thread?
And volatile means, that no cached values are used but before every operation, the value of the variable is read again?
Then, the concept of a a volatile variable is that the value a Thread reads from it is always up to date and now possibly outdated like a normal variable?
BUT, what about larger variables, like long or even objects?
Wouldn't it still be possible that while one thread reads the first byte of a long, another thread writes the second byte?
So, the first thread would still have an outdated value of the variable?
Yes, normally the JIT compiler can generate code that reads a value from memory into a register, then reuse that register everywhere without referring back to the original memory location.
However, even generating code that loads the memory location into a register every time may not help. The reason is that modern processors have a hierarchy of caches, and typically have a buffer of pending writes so it can combine small writes into one larger transaction. This is because the system memory has quite a lot of latency to open one memory location, but once it's open, a small number of subsequent locations can be accessed quite quickly. The overhead to keep all the caches always synchronized would be quite extensive, so it isn't done.
The result is that the processor will just read the value from its own cache rather than go back to main memory, unless you explicitly tell it that it must go back to main memory, and at the same time to tell other processors in the system to flush this location from their write buffer. This is referred to as a 'memory fence' or memory barrier[^].
Using the volatile keyword in C# will cause acquire and release barriers to be generated appropriately. In C and C++ the semantics only suppress compiler optimizations; they do not generate memory fences. The JIT compiler will generate the right code for the type you're using.
On x86 processors, since the Pentium, variables up to 64 bits in size that are aligned correctly (which they will be, by the JIT compiler, unless you use StructLayout attributes to explicitly set the alignment) are always read atomically, so you don't need to worry about partial reads of long variables. Pentium Pro and newer processors will perform unaligned reads atomically as long as it remains within a single cache line (the unit of access to cacheable areas of main memory).
For objects, volatile applies to the object reference, not the object itself. You cannot apply volatile to a user-defined structure.
See also Raymond Chen's article High-performance multithreading is very hard[^]. In general, stick to Monitor or the lock keyword (which just calls Monitor.Enter and Monitor.Exit for you) until you can prove that the lock is too much of a bottleneck.
DoEvents: Generating unexpected recursion since 1991
Thanks a lot, that was quite informative.
The main reason I made this topic is because I read aborting a thread by periodically checking a volatile bool is a good way to do it.
In the case of a bool, is it truly reliable that by the time the value is read, it is up to date?
Also, is there a better way to check for the cancellation request other than wrapping each 5-10 operations in an if statement?
And now I have a whole new problem, I was testing around with the lock keyword and noticed something I don't like at all.
//global variable accessible by all threads
static List<string> list = new List<string>();
static void Main(string args)
Thread t1 = new Thread(new ThreadStart(DoSomething));
Thread t2 = new Thread(new ThreadStart(DoSomethingElse));
//start thread t2, then t1
//Thread 1's work
static private void DoSomething()
//wait until thread 2 finished locking
for (int i = 0; i < 100; i++)
//first add "1" to the list and then write it to the console
//checks if this thread accesses the list before t2 finished its work
if (list.Count< 100 && list.Contains("2"))
Console.WriteLine("FIRST LOCK BREACHED!");
//writes thread number and count to the console
Console.WriteLine("1-Count: " + list.Count.ToString());
//Thread 2's work
static private void DoSomethingElse()
for (int i = 0; i < 100; i++)
//first add "2" to the list and then write it to the console
//checks if thread t accessed the list before the lock was released
Console.WriteLine("SECOND LOCK BREACHED!");
//writes thread number and count to the console
Console.WriteLine("2-Count: " + list.Count.ToString());
This will produce random output, it seems the threads get different time slices every time I execute the program, how come?
Also, sometimes it will produce this output:
FIRST LOCK BREACHED
SECOND LOCK BREACHED
This is confusing me, because I thought lock actually blocks other threads. However that is apparently not the case.
Thread 1 always starts after thread 2 and since thread 2 locks "list", thread 1 should have to wait until thread 2 is done, but apparently it slips in somewhere in between and still accesses the list.
By the time thread 2 writes to the list, it is empty but 3 lines below, when it checks if the list contains a "1", it really does. Why does that happen?
And I tried the same with wrapping all of thread 1's work in a lock of "list", in that case everything will be executed in order, no matter how many times I test it.
I'd really appreciate if you could clear this up.
Thank in advance.
Last Visit: 31-Dec-99 18:00 Last Update: 12-Jun-21 17:38