|
I'd say your approach is wrong. Constantly polling threads to see if they're done, aborting threads, using lots of synchronization events; these kind of things raise flags to me.
Let's step back for a moment. Ignoring possible implementation details, what are trying to accomplish?
|
|
|
|
|
Hello, and thank you for your reply !
Well, this is what i thought, but i don't know how to do that another way
I'm writing an interactive application, with a RichTextBox containing a script to process and get the answer to these lines. It has to handle debug features such as run, step by step, breakpoints, halt, and so on.
The thing is, i've got a main thread (my windows form), and i have to do two things :
1/ Have a background thread to listen to data input, that may come at anytime (i.e. even when the script is not processing, but have the possibility to disable it in order to process some specific commands)
2/ Have another thread to process the script lines, one by one or the whole script depending on the debug button pressed.
To me, i don't see another way to do that. Considering the reading thread for example, it has to loop for data input, unless i tell it to stop/pause. So i thought it was the right way to implement this feature.
Now after reading from you, i guess it's not right to start/stop constantly the threads, because for the step by step for example, i run the process thread just for one line and then i cut it off before the user asks for another interaction. I've also thought at using the events to pause the threads but it wasn't doing what i expected (maybe i've done some mistake on that implementation, i tried it in a hurry). But the thing is that i need to know when a thread is running, then have the ability to tell it to stop/pause and be sure it has done processing before performing the remaining operations.
As i have two loops (one for each thread), i can't find out another way that polling at each loop if they're asked to stop/pause.
I hope it's more clear for you now, and i also hope you can give me hand to find out how to implement that correctly !
Best Regards,
Mamat,
|
|
|
|
|
I've got a databound datagrid from SQL Server Express '05 and one of the columns is the foreign key to another table. Right now this column just displays a number (the ID) but I want it to display the name associated with that ID.
I'm sure that this is possible as I've seen it done with databound comboboxes.
|
|
|
|
|
Change the query for your datagrid to JOIN to the table that contains the names for IDs. Include the 'name' field in your query output and bind your grid column to this field instead of the ID field itself.
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
Paul you beauty! Thank you so much, you just made my week year!
|
|
|
|
|
No problem. Happy to help
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
Does anyone know how to best lock access to the object during enumeration? Eg, is it a bad/good idea to use a Monitor.Enter in the constructor and a Monitor.Exit in the dispose function? I know, I could put a lock around, but I really need this to happen automatically.
Cheers.
|
|
|
|
|
Check out this [e-book]. It has some really nice examples on (automatic) locking and when (not) to use it in chapter 2. You might want to look at the Mutex class.
Standards are great! Everybody should have one!
|
|
|
|
|
|
When you compile this code:
IEnumerable<string> Test() {
lock (someObj) {
yield return "a";
yield return "b";
}
}
Then the C# compiler creates an enumerator class that calls Monitor.Enter on the first MoveNext call; and calls Monitor.Exit in the third MoveNext call, or in the Dispose method if Dispose is called between after the first MoveNext call and before the third.
foreach will automatically dispose the enumerator, but I've seen people write code like
IEnumerable<MyType> myCollection = ...;
if (e.GetEnumerator.MoveNext()) {
So yes, using Monitor.Exit in the Dispose function is the way locking is meant to happen in enumerators, but make sure that your team is aware of the fact that enumerators must be disposed!
|
|
|
|
|
I am trying to find every control on my page with "txt" in the id, they are all my textboxes. The following bit of code is working fine and duly goes into the inner condition for some of the textboxes on the screen. But not all.....will this only work on textboxes that are visible at runtime as I have some sent to txtBox.visible = false at startup.
foreach (Control c in Page.FindControl("MyPage").Controls)
{
if (c.ID != "" && c.ID != null && c.ID.ToString().IndexOf("txt") > -1)
{
TextBox txtClearBox =new TextBox();
txtClearBox.ID = c.ID;
txtClearBox.Text = "";
}
}
Any advice on clearing all my textboxes would be great.
Thanks in Advance
|
|
|
|
|
Instead of checking that the name isn't null, and that it contains "txt"
Can you not use Control.GetType() and see if it is a textbox?
And... are you making a new textbox everytime you want to clear it? Why not just set the text to ""?
My current favourite word is: PIE!
Good ol' pie, it's been a while.
|
|
|
|
|
No, not a new textbox, just want to clear the existing textboxes when event 'A' or 'B' happens. There are a few and obviously I could say txtBoxA = "";txtBoxB = "" etc, there are about 20.
I'm gonna try Control.GetType now, I'll let you know, thanks for the quick response Undefeated.
Harvey
|
|
|
|
|
this should do it (untested).
foreach (Control c in Page.FindControl("MyPage").Controls)
{
if (c is TextBox)
{
c.Text = String.Empty;
}
}
"On one of my cards it said I had to find temperatures lower than -8. The numbers I uncovered were -6 and -7 so I thought I had won, and so did the woman in the shop. But when she scanned the card the machine said I hadn't.
"I phoned Camelot and they fobbed me off with some story that -6 is higher - not lower - than -8 but I'm not having it."
-Tina Farrell, a 23 year old thicky from Levenshulme, Manchester.
|
|
|
|
|
thanks guys, I've got to that point, that works great. However the code does not find any of the textboxes if they are in an asp:panel
<asp:label id="lblRaisedBy">Raised By
<asp:dropdownlist ="ddlraisername"="">
<asp:textbox id="txtOrganisation">
<asp:panel id="pnlRaiser" runat="Server" visible="True">
<asp:label id="lblOrg" runat="server" width="7em" cssclass="NonMandatoryLabel" visible="true">Organisation
<asp:textbox id="txtRaiserName">
txtOrganisation is found whereas txtRaiserName isn't. Why is the panel causing a problem?
|
|
|
|
|
Because the textbox is a control on the panel, not the form, so you'd have to do this:
foreach(Control c in Page.FindControl("MyPage").Controls) {
if(c is TextBox)
c.Text = "";
if(c is Panel)
foreach(Control d in c) {
if(c is TextBox)
c.Text = "";
}
}
And loop through each control in the panel
My current favourite word is: PIE!
Good ol' pie, it's been a while.
|
|
|
|
|
It seems the foreach inside each other doesn't work..
foreach statement cannot operate on variables of type 'System.Web.UI.Control' because 'System.Web.UI.Control' does not contain a definition for 'GetEnumerator', or it is inaccessible
Is that right?
|
|
|
|
|
Oops, made a slight error it should be:
foreach(Control d in c.Controls) {<br />
...
And it then should of course be:
if(d is textbox)
...
not if(c is textbox)
foreach(Control c in Page.FindControl("MyPage").Controls) {
if(c is TextBox)
c.Text = "";
if(c is Panel)
foreach(Control d in c.Controls) {
if(d is TextBox)
d.Text = "";
}
}
My current favourite word is: PIE!
Good ol' pie, it's been a while.
|
|
|
|
|
Hey, that works like a dream, thanks a lot for that.
I now know something I didn't, what more can I want.
Thanks again.
Harvey
|
|
|
|
|
Hi all,
I would like to know how I can get out of this loop the first time the condition is true?
public string DirSearch(string sDir)
{
try
{
string compareString = "";
foreach ( string dir in Directory.GetDirectories(sDir))
{
foreach ( string file in Directory.GetFiles(dir))
{
compareString = file.Substring(file.LastIndexOf("\\"),(file.Length-file.LastIndexOf("\\")));
if (globalFileName == compareString.Substring(1, (compareString.Length - 1)))
{
return file;
}
}
DirSearch(dir);
}
return "";
}
catch (System.Exception excpt)
{
MessageBox.Show(excpt.Message,"Exception Occured",MessageBoxButtons.OK,MessageBoxIcon.Error);
return "";
}
}
Many Thanks
Regards,
The only programmers that are better that C programmers are those who code in 1's and 0's
Programm3r
My Blog: ^_^
|
|
|
|
|
AFAIK the only way to break out of foreach look is with a "break" statement.
There are some interesting discussions, and yes blogs, around the net about foreach versus for loops in .NET. You might want to check them out.
|
|
|
|
|
Hi Mike,
Thanks for your reply and comments but, break does not work. I have tried it without success.
Regards,
The only programmers that are better that C programmers are those who code in 1's and 0's
Programm3r
My Blog: ^_^
|
|
|
|
|
Programm3r wrote: Thanks for your reply and comments but, break does not work. I have tried it without success.
works for me
string[] test = {"help","me","with","for","loops"};
foreach(string t in test)
{
Console.WriteLine(t);
if( t.Equals("for"))
break;
}
|
|
|
|
|
Programm3r wrote: if (globalFileName == compareString.Substring(1, (compareString.Length - 1)))
{
/* File was found */
return file;
}
if you mean this bit, it already does by virtue of the return keyword.
In general you can break a look by using the break keyword. For completeness you may also like to look up the continue keyword in c#
|
|
|
|
|
you can use also "goto"
short example:
string filename = "";
for(int i = 0; i < 10; i++)
{
if(i == 3)
{
filename = "path"
goto FileHasFounded;
}
}
FileHasFounded:
MessageBox.Show("file name is this: " filename);
break don't work in this case because you have two loops. and it will break only from one loop
hope it will help.
respect.
spaps
|
|
|
|