|
Hi,
Control.InvokeRequired is the correct way to deal with this.
You can hide it in a helper class, that is what Avoiding InvokeRequired[^] (a very recent article here on CP) does for you, however I'd rather use code that works straight with InvokeRequired.
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
|
|
|
|
|
Luc Pattyn wrote:
You can hide it in a helper class, that is what Avoiding InvokeRequired[^] (a very recent article here on CP) does for you, however I'd rather use code that works straight with InvokeRequired.
Hmm I see what you mean (i sorta missed what you said when i first read your post). I read this article and have spent the last couple of hours playing with the demo. It really does just move the problem. May be better tools for this problem should be added to .NET 4.0 (hell its bloated anyway).
I dont know if its the intense heat (some 30C here in the UK), the fact that I am tired, or the fact that I'm watching family guy marathon on DVD, but ive taken the easy way out.
First in MyClass.cs I have added the following property:
public Form Sender { get; set; }
now when a method is called, just before an event is raised, the sender form is checked for InvokeRequired.
if (Sender.InvokeRequired)
{
var dataDelegate = new Data(LiveStats_OnData);
Sender.Invoke(dataDelegate, new object[]{sender,e});
}
else
{
}
the horrible nasty InvokeRequired is now tucked away and abstract so the user never sees it.
Bailed. Please Microsoft, give us a break.
|
|
|
|
|
Hi,
I'm not convinced you need to go for all that sender trouble: when all Controls got created by the main thread (that's the safest way to do WinForms) then whatever Control you ask for InvokeRequired will return the same value: false on the main thread, true otherwise. So a single Form or Control is all you need (and then you could also use the context stuff, which I haven't used yet).
PS: it's the same 30C in B.
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
|
|
|
|
|
Hi,
The System.ComponentModel.BackgroundWorker class raises it’s events on UI thread and would suit your requirements as you only require a task completed event.
In general terms it is possible to arrange for an event to be raised on the UI thread through use of either the Post or Send methods of the SynchronizationContext class or alternatively the Post method of the AsyncOperation class.
I’ve pasted in an example class that shows one way of doing this.
Alan.
using System;
using System.ComponentModel;
using System.Threading;
public class BackGroundTask {
private SynchronizationContext context;
public event EventHandler<AsyncCompletedEventArgs> Completed;
public void RunAsync(Object args) {
this.context = AsyncOperationManager.SynchronizationContext;
ThreadPool.QueueUserWorkItem(new WaitCallback(this.ThreadProc), args);
}
private void ThreadProc(Object args) {
Exception exc = null;
Boolean cancelled = false;
try {
} catch (Exception e) {
exc = e;
} finally {
this.OnCompleted(exc, cancelled, null);
this.context = null;
}
}
private void OnCompleted(Exception e, Boolean cancelled, Object state) {
this.context.Post(
delegate {
EventHandler<AsyncCompletedEventArgs> handler = this.Completed;
if (handler != null) {
AsyncCompletedEventArgs args = new AsyncCompletedEventArgs(e, cancelled, state);
handler(this, args);
}
},
null);
}
}
|
|
|
|
|
I love the way these pages look. How you have a list of all the posts, can click on one, and the whole post is displayed. I am in the process of doing something similar. I'm building a website that will show 5 of say 17 fields from a selected record in GridView kind of like to get the user's attention. Then, when the user clicks any of the 5 fields, all 17 fields would drop down, much like this page. Can that be done only with GridView or would I have to add a View/MultiView to the page. If I can do it with GridView is there a property I can set or would I have to write custom code? Are there any good code snippets out there?
THanks.
|
|
|
|
|
mjc225 wrote: If I can do it with GridView is there a property I can set or would I have to write custom code?
I believe that you've to write custom code.
Moim Hossain
R&D Project Manager
BlueCielo ECM Solutions BV
|
|
|
|
|
Hi all
Have started writing a Class to help me with a problem in my current project and I would like to be able enumerate the variables in the Class by using a 'foreach(object o in MyClass)' in my main program.
I was getting a 'MyClass does not contain a public definition for GetEnumerator' but have worked out why and I am at the point where I could really do with some help with the GetEnumerator function.
public class MyClass : IEnumerable
{
public System.DateTime datStart;
public System.DateTime datEnd;
public int intParam1;
public int intParam2;
public MyClass()
{
datStart = new DateTime(1900, 1, 1);
datEnd = datStart;
intParam1 = -1;
intParam2 = -1;
}
IEnumerator IEnumerable.GetEnumerator()
{
return null;
}
}
I know there are many, (many, many,) references to this on the web but I haven't been able to re-work any of them to solve my specific problem.
Any help would be MUCH appreciated.
Thank you.
|
|
|
|
|
You seem to misunderstand the concept of enumerators:
You don't enumerate a single class, but you enumerate over collections like e.g. List or Dictionary .
Not quite sure what you are trying to do, but IEnumerable is definitely not the way...
Regards
Thomas
www.thomas-weller.de
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Programmer - an organism that turns coffee into software.
|
|
|
|
|
Thomas
Thank you for taking the time to reply to my post.
I am trying to implement foreach(object o in MyClass) for reporting and (possibly) serialization.
|
|
|
|
|
You could use reflection to enumerate over all the desired members of a class - like described in the post below. Alternatively simply implement a method object[] GetAllObjects() that returns everything you need. For serialization use the Serializable attribute and eventually the ISerializable interface (see here^ and here^).
Regards
Thomas
www.thomas-weller.de
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Programmer - an organism that turns coffee into software.
|
|
|
|
|
Thomas
I will read up on reflection. It seems to be what I was after.
Thank you.
|
|
|
|
|
Hi,
As Thomas said enumerators is not what you need here.
You need Reflection, which can give you access to all the data fields, constructors, methods, properties of your class.
This is an example listing all the known colors in the SystemColors class:
Assembly asm=Assembly.GetAssembly(Color.White.GetType());
Type type=asm.GetType("System.Drawing.SystemColors", true);
PropertyInfo[] pis=type.GetProperties();
int n=pis.Length;
log("There are "+n+" properties");
foreach (PropertyInfo pi in pis) {
Color c=(Color)pi.GetValue(null, null);
log(pi.Name+" = "+c.ToString());
}
BTW: you can only get information on class members, not on local variables inside some method.
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
|
|
|
|
|
Thank to all that responded to my post.
Luc, I think I can re-work your example to give me what I was after.
Thank you.
|
|
|
|
|
Luc, I managed to re-work your example to give me just what I was after.
Thanks for putting me on the right track:
private void ShowData()
{
Type type = typeof(MyClass);
FieldInfo[] fi = type.GetFields();
foreach (FieldInfo info in fi)
{
Debug.WriteLine(info.Name + " = " + info.GetValue(varMyClass));
}
}
varMyClass is a variable of type MyClass.
Thanks to all who helped.
modified on Thursday, July 2, 2009 1:41 PM
|
|
|
|
|
A quick google found this[^] which may be along the lines of what you're looking for?
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
public class MyClass : IEnumerable<put type here> //IEnumerable<t> is better (no boxing)
{
public System.DateTime datStart; //Schedule start date
public System.DateTime datEnd; //Date of final occurrence (#1/1/1900# if N/A)
public int intParam1; //Various Uses
public int intParam2; //Various Uses
//Other fields remove for clarity
//Constructor
public MyClass()
{
//Set defaults for new MyClass()
datStart = new DateTime(1900, 1, 1);
datEnd = datStart;
intParam1 = -1;
intParam2 = -1;
}
public IEnumerator<put type here> GetEnumerator()
{
yield return someStuff; //put more of them here possible in a loop, the compiler will make a state machine out of it
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator(); //this does not loop, it calls the other GetEnumerator
}
}
|
|
|
|
|
Can you use threading without a static method?
|
|
|
|
|
Hi,
the Main() method of an app has to be static, that is where the app starts.
Apart from that, no method has to be static.
I typically create a "Job" class that holds the job's parameters and then creates a thread and runs it,
nothing static in there.
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
|
|
|
|
|
Wogboiii wrote: Can you use threading without a static method?
I don't see any static methods other than main in the example code in the documentation for BackgroundWorker[^], do you?
|
|
|
|
|
Or in the BGW's code[^].
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Sure...
The static keyword has nothing to do with how the code is executed, but only with where it lives (instance or type). Static/not static is simply irrelevant when it comes to different threads.
In the end, it's all bytes that run through the processor, no matter where they come from...
Regards
Thomas
www.thomas-weller.de
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Programmer - an organism that turns coffee into software.
|
|
|
|
|
I think the original poster saw an example using a static method to start a new thread and thought it was a requisite.
|
|
|
|
|
Hi experts
i am using visual studio 2005 and sql server 2005
i am working on winform.when i want to fill the datagrid view simultaneous
show the .gif picture that show loading of grid.
but when i run it gif is not working. it run work when the grid fill Completely.then how i can fix this problem so that both grid and .gif work
simultaneous.
Thank u
Dinesh
|
|
|
|
|
Do you mean "show the .gif picture that show loading of grid." use the .gif like a progress bar, or similar to the images shown during a file copy operation?
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
look at the BackgroungWorker[^] component
What the issue is is that your form is not being updated until the load code has completed. You could try Form.Refresh(); every now and then during your loading. But I would strongly suggest you go for a background worker
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|