I'm just curious: How would you achieve what I'm after with a separate thread? I like the idea but they're scary :-) Would I just use Thread.Sleep in that thread instead of a timer? Thanks!
xkrja
Posts
-
Stop all threads in Threading.Timer in Callback method? -
Stop all threads in Threading.Timer in Callback method?As I've understand it the callback method specified in the Timer constructor can be executed multiple times in parallell. It does not wait for the callback to finish. This can cause issues for me so I need to be sure that only one callback (or the task it will perform) will run one at a time and if the task suceeds the timer must stop. How can I do that? Will the following do the job?
System.Threading.Timer timer; private void StartThreadTimer() { timer = new System.Threading.Timer(TimerCallback, "hepp", 0, 5000); } void TimerCallback(object callbackObj) { if (Monitor.TryEnter(lockerObject)) { try { Thread.Sleep(1000); // If this task succeeds the timer should stop and no callbacks must be allowed to be called again timer.Dispose(); //Can this guarantee that no more callbacks will be called? } finally { Monitor.Exit(lockerObject); } } }
If the above is not safe enough, how should I do it? Thanks! -
Quick Linq to XML questionI'm starting out with linq to xml and have a quick question: I have the following xml-file: <countries> <country code="AF" iso="4">Afghanistan</country> <country code="AL" iso="8">Albania</country> <country code="DZ" iso="12">Algeria</country> <country code="AS" iso="16">American Samoa</country> <country code="AD" iso="20">Andorra</country> . . . How can I get a result containing all the country names by using a linq expression? I tried the following but it only gives me "Afghanistan": XDocument xmlDoc = XDocument.Load("countries.xml"); var countries = from country in xmlDoc.Descendants("countries") select new { Name = country.Element("country").Value, }; Thanks for help!
-
Get Results View from ICollectionView?Is it possible to get the "Results View" from the ICollectionView? If so, how? I don't want the "SourceCollection" but instead the "Results View". Thanks for help!
-
Synchnroization with lock statementI found it just before I saw your answer :-) It seems there are two solutions: ReaderWriterLock and ReaderWriterLockSlim. It seems to exactly what I need! Thanks for your help!
-
Synchnroization with lock statementThanks for the reply! You're right. I did a test and tried it out. But is there some way around this? So while a non-changing operation is performed on the list other non-changing operations can get access to the list as well? So that only modifications to the list can be stopped while theese non-changing operations is happening to the list? The reason is I'm creating a WCF service and each client contacts the service on a different thread. Each time a clients calls a method on the service the list is searched. This means if several hundreds of clients are connected every thread is locking the list and it takes some time to search there will be a real performance decrease. A solution I guess is to wrap all non-changing operations in try-catch blocks and skip the lock but that seems like a real hack! EDIT: It's a static list we're talking about in this case. Thanks again!
-
Synchnroization with lock statementA couple of days ago I asked here how to ensure that a generic list isn't modified while an operation like Find() or Exist() is performed on the list. I was recommended to use the lock-statement. Now I would like to know if it's enough to lock the list while adding or removing items from it? Or do I need to lock it while performing a non-changing operation like Find() on the list as well? Thanks for help!
-
Removing items from list while performing operations on itI would like to know what happens if objects are removed from a list while operations are performed on this list? Let's say we have this class:
class MyListObject
{
int MyInt {get;set;}
string MyString {get;set;}
}And we have a list of "MyListObject" called "MyList". We also have an eventhandler that can trigger randomly. In that eventhandler a random item in "MyList" is removed from "MyList". Now, what happens if I do something like:
MyList.Find(delegate (MyListObject mlo) {return mlo.MyString == "hello";});
And at the same time we perform this operation on the list the event is triggered and the eventhandler removes some item from the list. Will that throw some kind of exception? I don't know how to test it so that's why I'm asking you guys. If this can cause problems, how can I get around this? Thanks for help!
-
App.config with custom section and custom elements collection insideI want to set up a config section in my app.config that looks something like below:
<ConfiguredDatabases> <Database Type="MyFirstDatabase" /> <Database Type="MySecondDatabase" /> <Database Type="MyThirdDatabase" /> </ConfiguredDatabases>
But I don't understand how to the custom configuration class need to look like in order to get this to work. I found the below link: http://devlicio.us/blogs/derik_whittaker/archive/2006/11/13/app-config-and-custom-configuration-sections.aspx[^] But in that sample it looks like below:<StartupFolders> <Folders> <add folderType="A" path="c:\foo" /> <add folderType="B" path="C:\foo1" /> </Folders> </StartupFolders>
You see that there is a difference: The element names in the collection are named 'add'. I want the to have a custom name (like 'Database'). It's more clear to me. How would I proceed to achieve that? Thanks for help! -
Export metadata from C# methods and classesHow can I create an API document from my C# metadata information (methods and class name descriptions etc.)? Not pure C# related perhaps but anyway. Thanks for help!
-
Inheritance questionThanks for the reply. But the problem for me is that I call methods in the dll I mention and they return the type 'DataProvider.Trend.Tag' BUT I need to cast that to type 'Tag'. I don't want to cast 'Tag' to 'DataProvider.Trend.Tag' (the reason is that 'Tag' so that it is accepted by a Web Service). Thanks again.
-
Inheritance questionI've referenced a dll made in VB.Net and now I need to inherit from some of the classes in the dll. Below is an example of how I did that:
class Tag : DataProvider.Trend.Tag
{
}Now if I do like this:
DataProvider.Trend.Tag myBaseTag = new DataProvider.Trend.Tag();
Tag myTag = myBaseTag as Tag;this results in that 'myTag' gets the value null. Why is that? How can I assign the value of myBaseTag to myTag? Thanks for help!
-
3rd party control for wpf graphic editingThanks for the reply. No, I mean like a control that you can embed in your own project. Perhaps something with xaml editing possibilities. Like a xamlPad control that can be embedded. Is there such a thing? Thanks again!
-
3rd party control for wpf graphic editingI'm just starting out with a WPF-project and I need to know if there are any controls available that can be used as a WSIWYG editor for xaml objects. It should be something that can be referenced to our project and provide graphic editing for basic xaml objects. Anyone know anything about that? Thanks for help!
-
Prevent the ^ character from beeing entered in textbox.Thanks for the replies guys. Sorry, but I forgot to mention that I work in Silverlight. I can't get to the KeyPress event. Only KeyDown and KeyUp. Thanks again.
-
Prevent the ^ character from beeing entered in textbox.How can I prevent the ^ character from being typed in a textbox? The problem seems to be that the character is not added before another keydown/keyup event. For some other keys I used the following code to prevent them from being typed:
void textBox_KeyUp(object sender, KeyEventArgs e)
{
if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
{
if (e.PlatformKeyCode <= 12)
e.Handled = true;switch (e.PlatformKeyCode) { case 226: e.Handled = true; break; } } else if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift) { switch (e.PlatformKeyCode) { case 52: e.Handled = true; break; case 186: e.Handled = true; break; } } }
However, this does not work for the ^ character. Thanks for help!
-
Get what object a button belongs to?Thanks for the reply! I think that was exactly what I was looking for. I'll try to implement this in my real case and I get back if I don't succeed.
-
Get what object a button belongs to?Thanks for the reply. Yes, I know I can get the object that raised that event. But that object belongs to an object 'myClass' which in turn contains 'myClass.MyInt'. Is it possible to get which of the 'myClass.MyBtn' that raised the event. If I can get that I would also be able to read out the value of 'myClass.MyInt'
-
Get what object a button belongs to?I will try to shorten this question. If I have the following class:
public class MyClass
{
public int MyInt {get; set;}
public Button MyBtn {get; set;}
}Now, let's say I have a for-loop that generates several objects of this class and hooks it up to an event handler like this:
for (int i = 0; i < 5; i++)
{
MyClass myClass = new MyClass();
myClass.MyInt = i;
myClass.MyBtn = new Button();
myClass.MyBtn.Click += (s, e) =>
{
//Is it possible to get the value of 'myClass.MyInt'
//in this event handler since 'myClass.MyBtn' belongs
//to the same object?
};
}So, my question is: Is it possible to get the value of 'myClass.MyInt' when the button is clicked? I mean, 'myClass.MyBtn' and 'myClass.MyInt' belongs to the same object. The problem is that I must hook up all buttons to the same event handler. Thanks for help!
-
regular expressionsThanks for your help. I'll take a look at it!