|
I'm having a bit of trouble with SLEEPing a thread in c#.
I have the following enum used to control how long the thread should sleep:
public enum RefreshRate{<br />
fast = 100, <br />
medium = 500,<br />
slow = 1000<br />
}
but the line:
oThread.Sleep(RefreshRate);
Generates the following error:
Argument '1': cannot convert from 'RefreshRate' to 'int'
The default type for an enum is INT, so I'm not sure what's causing the problem.
Anyone have any ideas?
TIA.
Mike Stanbrook
mstanbrook@yahoo.com
|
|
|
|
|
It's still an enum of type RefreshRate. What you need to do is put:
oThread.Sleep((int)RefreshRate.fast);
I don't know whether it's just the light but I swear the database server gives me dirty looks everytime I wander past.
-Chris Maunder
Microsoft has reinvented the wheel, this time they made it round.
-Peterchen on VS.NET
|
|
|
|
|
David Stone wrote:
oThread.Sleep((int)RefreshRate.fast);
You don't necessarily need to cast the enum; this should work explicitly when you identify the base type of the enum:
public enum RefreshRate<code> : int</code>
{
fast = 100,
medium = 500,
slow = 1000
}
oThread.Sleep(RefreshRate.fast);
Nick Parker
May your glass be ever full.
May the roof over your head be always strong.
And may you be in heaven half an hour before the devil knows you’re dead. - Irish Blessing
|
|
|
|
|
That, and I just noticed that he was doing oThread.Sleep(RefreshRate);
He wasn't specifying a value at all.
I don't know whether it's just the light but I swear the database server gives me dirty looks everytime I wander past.
-Chris Maunder
Microsoft has reinvented the wheel, this time they made it round.
-Peterchen on VS.NET
|
|
|
|
|
I have a main thread on which GIU Controls all live, and when the user clicks a button, I have a worker thread start doing calculations. While the worker thread does calculations, the main GUI thread is free to let the user click around in the app, look at tab pages, etc..
When the worker is done, I want it to throw an event to tell the main GUI thread that it's finished. As I understand it, this isn't possible.
When the worker thread raises the event, and a class on the main GUI thread is listening for it with an eventhandler, the eventhandler does run. But it doesn't run on the main gui thread. It doesn't run on the worker thread either. It runs on some new third thread. I have the worker thread use BeginInvoke to raise the event.
How do I make a worker thread raise an event on the main gui thread instead of creating a third thread?
"Outside of a dog, a book is Man’s best friend. And inside of a dog, it’s too dark to read."
-Groucho Marx
|
|
|
|
|
Short answer, you don't BeginInvoke will cause it to run on a thread from the ThreadPool; Invoke (or just calling the delegate like any other method) will cause it to run in the same thread (ie the worker thread).
What you do is use the (Begin)Invoke method from the form instance to have it run on the same thread as the GUI.
My preferred way of doing this is to use the handler like you already are but make the [edit]form handler[/edit] be responsible for calling Invoke on itself.
theThread.theEvent += new EventHandler(this.ThreadIsDone);
....
private void ThreadIsDone(object sender, EventArgs e)
{
if( InvokeRequired )
{
Invoke(
new EventHandler(this.ThreadIsDone),
new object [] { sender, e }
);
return;
}
}
James
- out of order -
|
|
|
|
|
Thanks this is great, I sort of understand why it will work. The only thing is in my case, the object that I have listening for the event isn't a Form, (or any other Control). When it was instantiated, it was created on the main gui thread where the form and controls are, but it isn't a Control itself. So there's no Invoke() method or InvokeRequired within it. I suppose I could make it a Control if I have to, just to get that method.
Is there like an interface or something I can have the object use so that it will have the Invoke() method?
thanks again
"Outside of a dog, a book is Man’s best friend. And inside of a dog, it’s too dark to read."
-Groucho Marx
|
|
|
|
|
Hmmm, that is a slight problem; but I'm curious if it isn't a GUI component then why does it need to run on the GUI thread? If it modifies a Control's properties or calls a Control's methods then can just use that particular control's InvokeRequired property and Invoke method to do the work.
Bog wrote:
Is there like an interface or something I can have the object use so that it will have the Invoke() method?
None that I know of.
James
- out of order -
|
|
|
|
|
James T. Johnson wrote:
I'm curious if it isn't a GUI component then why does it need to run on the GUI thread?
The object isn't a GUI control, but in it's eventhandler for the "I'm finished" event, it creates some GUI components and adds them to some tabpages on the form. To create and add the GUI components, it needs to be one the same thread as the GIU.
Hey but your solution gave me lots of ideas, I think I can do this, thanks very much James.
btw I found the interface, it's ISynchronizeInvoke.
"Outside of a dog, a book is Man’s best friend. And inside of a dog, it’s too dark to read."
-Groucho Marx
|
|
|
|
|
I am inheriting my class from IList as suggested earlier as a solution to my DataGrid woes. However, it struck me that I would think that it would be best if the indexer, a required property for inheriting from IList , was type-safe, i.e. returned a MyType instead of a simple object . But when I do this:
public MyType this[int index]
{
get { return (MyType)this.myArrayList[index]; }
set { return this.myArrayList[index] = value; }
}
object this[int index]
{
get { return this.myArrayList[index]; }
set { return this.myArrayList[index] = value; }
}
it gives me this: Class 'MyIListImplementor' already defines a member called 'this' with the same parameter types .
Yet I know the framework classes seem able to create indexers with non-object return values, yet still inherit from IList . Help?
-Domenic Denicola- [CPUA 0x1337]
“I was born human. But this was an accident of fate—a condition merely of time and place. I believe it's something we have the power to change…”
|
|
|
|
|
It does seem like its impossible, but it isn't (thankfully)
C# (and .NET) supports the ability to specify which methods/properties should be used to implement an interface.
To do so, precede the method/property name with the name of the interface and drop the visibility modifier (because interface methods must always be public).
In your case...
public MyType this[int index] {
get { return (MyType)this.myArrayList[index]; }
set { return this.myArrayList[index] = value; }
}
object IList.this[int index] {
get { return this.myArrayList[index]; }
set { return this.myArrayList[index] = value; }
} As an aside, if you're using an ArrayList for the underlying implementation why not inherit from CollectionBase and not have to worry about implementing IList, ICollection, etc. You just need to worry about the typesafe methods for your collection.
James
- out of order -
|
|
|
|
|
Thank you very much! This works great!
James T. Johnson wrote:
As an aside, if you're using an ArrayList for the underlying implementation why not inherit from CollectionBase and not have to worry about implementing IList, ICollection, etc. You just need to worry about the typesafe methods for your collection.
Right... I knew that... really
Methinks my lack of FCL knowledge has made me do a lot more work than I meant to. Thanks for this as well!
-Domenic Denicola- [CPUA 0x1337]
“I was born human. But this was an accident of fate—a condition merely of time and place. I believe it's something we have the power to change…”
|
|
|
|
|
Domenic [Geekn] wrote:
Methinks my lack of FCL knowledge has made me do a lot more work than I meant to.
You are better off for it though Now you know that it exists and you also learned how you can implement IList in a strongly-typed collection.
James
- out of order -
|
|
|
|
|
Wow James, you never fail to amaze me
"I dont have a life, I have a program." Also, I won't support any software without the LeppieRules variable.
|
|
|
|
|
Ditto here. My only problem was when he said this:
It does seem like its impossible, but it isn't (thankfully)
He didn't use the emoticon! He should have put the extra dash in there. Seriously James, you're an editor! I am really disappointed.
I don't know whether it's just the light but I swear the database server gives me dirty looks everytime I wander past.
-Chris Maunder
Microsoft has reinvented the wheel, this time they made it round.
-Peterchen on VS.NET
|
|
|
|
|
David Stone wrote:
He should have put the extra dash in there.
I've gotten too used to Trillian's emoticons, I'll go hang my head in shame now.
James
- out of order -
|
|
|
|
|
It's okay James. I forgive you. Now go make a pilgrimage to the shrine of and sacrifice a Mac on the altar.
I don't know whether it's just the light but I swear the database server gives me dirty looks everytime I wander past.
-Chris Maunder
Microsoft has reinvented the wheel, this time they made it round.
-Peterchen on VS.NET
|
|
|
|
|
Dear Masters of .NET,
Does anyone have proper sample or documentation about
creating a custom forms designer like VS.NET's or SharpDevelop's designer.
I have search the NET for quite a while, but no success.
I also dug the SharpDevelop's code and found that without documentation it
is very hard to see what the developers have done.
Any help is welcome and appreciated.
C:\>csc *.cs
Microsoft (R) Visual C# .NET Compiler
error CS2001: Source file 'brains.cs' could not be found
fatal error CS2008: No [brains.cs] specified
C:\>
|
|
|
|
|
This is a complicated task, and understanding the SharpDevelope code isn't an unreasonable way to begin your task.
If, on the other hand, you are not actually trying to duplicate all of the functionality, (maybe you only want labels, text boxes, and buttons on your forms), you could try simply writing the thing from scratch in a way that seems logical to you. This method requires using the container form's .Capture property in order to catch the clicks that normally would have set the focus to the child control, allowing you to select and move the controls.
John :D
|
|
|
|
|
Since it's really easy to access resources and modify them in .net, what do people think about storing application configs inside of the .exe itself (maybe storing the first date the program was ran inside of the executable itself, granted it's easy to modify, but I'm sure other methods will be used as well). Anybody have any thoughts on this? good idea? bad idea? I just think it might be nice to have an executable with no attachment to the registry and no other required files (sans runtimes).
|
|
|
|
|
To my opinion the general purpose of such configuration file is the ability
to manipulating an application’s behavior whenever necessary.
I am not sure if you could modify a .NET exe/dll file without reconstructing.
Perhaps it is possible to use Soap/Binary Serialization in combination with PropertyGrid and a command line argument to do the trick. This way you create a “PropertyGrid enabled” class that holds the configuration, then you Serialize and/or DeSerialize the class when the application starts with a startup argument like /conf
Any way, this is my opinion.
C:\>csc *.cs
Microsoft (R) Visual C# .NET Compiler
error CS2001: Source file 'brains.cs' could not be found
fatal error CS2008: No [brains.cs] specified
C:\>
|
|
|
|
|
Okay, I am unable to find all the info on this.
We are creating dynamic C# code with the assembly files and project file.
I want to pass the project to the C# compiler programmatically.
What are the steps I need to take to accomplish this?
I am at a loss as to even where to start! I'd be happy if someone can even just point me to a tutorial on this!
Thanks much fellow CP'ers.
_____________________________________________
The world is a dangerous place. Not because of those that do evil, but because of those who look on and do nothing.
|
|
|
|
|
System.CodeDom.Compiler & System.Reflection.Emit
Its hard, very hard to understand, but once u grasp it, its pretty easy. Look on dotnet247
"I dont have a life, I have a program." Also, I won't support any software without the LeppieRules variable.
|
|
|
|
|
Thanks for the input.
Once I get it all figured out....it sounds like a good article to publish!
Gee...I might actually get published here.
_____________________________________________
The world is a dangerous place. Not because of those that do evil, but because of those who look on and do nothing.
|
|
|
|
|
C# also has a managed compiler, but I havent used that yet.
Microsoft.CSharp.Compiler class
"I dont have a life, I have a program." Also, I won't support any software without the LeppieRules variable.
|
|
|
|