i have a condtion if (!e.ComponentUp) ,if its true then i need to wait until it Up what check i can give here
I did this code but its not working
if (!e.ComponentUp)
{
do
{
while (e.ComponentUp);
}
}
Your code cannot work.
First, if the predicate is false, then the while loop will exit immediately. You would need to loop on the negation.
Second, unless it's volatile and being changed asynchronously, the loop won't end.
Third, this sort of polling will max out the CPU.
Use an event.
You could create an event on your Component called something like 'UpChanged' and subscribe to the event. When you come to the code that can only run when the component is in the 'Up' state you could set a flag that indicates your 'task' is waiting for the up state to change. Something like this:
//When you initialize
var e = ...;
e.UpChanged += new EventHandler(Component_UpChanged);
bool waitingForUp = false;
//The code snippet that is waiting for up to be enabled
if (!e.ComponentUp)
waitingForUp = true;
else
DoWorkHere();
//The delegate
void Component_UpChanged(object sender, EventArgs e)
{
if (e.ComponentUp && waitingForUp)
DoWorkHere();
waitingForUp = false;
}
Why don't you use events ? Is that a 3rd party component ?
Related
I Make WinForms App for execute ssis package , and i want to make timer with datepicker so when i choose datepicker at 5pm the method will be executed at 5pm. I Have difficulty to make that happen first i tried this with do while but it will just stop executing when the statement is false and will always executing when statement is true, what i want to create is otherwise, i want to the looping is like checking not executing , so it will always looping for checking and will be executed only when statement is true
do
{
Executed();
}
//Label3 is DateTimeNow
while (dateTimePicker1.Text == label3.Text);
so the logic is something like that, but the opposite way, the code will looping until the statement is true when normally the statement start from true and will stop looping when statement false, and what i try to achive is the statement is start from false and will keep checking till statement is true. Can we do that ? thanks
Instead of having a loop to check whether the value of DateTimePicker is desired, you should use a Timer and handle its Tick event to check if the DateTime.Now is the desired time:
bool handled = false;
private void timer1_Tick(object sender, EventArgs e)
{
var d1 = DateTime.Now;
var d2 = dateTimePicker1.Value;
if (d1.Year == d2.Year && d1.Month == d2.Month && d1.Day == d2.Day &&
d1.Hour == d2.Hour && d1.Minute == d2.Minute)
{
if (!handled) //Allow running once
{
handled = true;
DoSomething();
}
}
}
Note 1: To make above example working, make sure you set the Interval property of the timer to 30000 to run every 30 seconds and also Start the timer whenever you want the scheduler start monitoring. Then the DoSomething method will run at exact time (up to minute level) which you set using DateTimePicker. You can change the criteria based on your requirement, for example to make it run every day at the specified time, you can remove d1.Day == d2.Day.
Note 2: :Scheduling libraries like Quartz are created to let you create scheduled task in your application easily.
You can use the not operator "!"
while (dateTimePicker1.Text != label3.Text);
As the title suggests, I'm new to the concept of threading and I'm learning as I go. I'm working on a program as part of a research project involving a 3D printer. I have two pieces of hardware I need to interface with: a small stage that moves up and down, and a force sensor that can detect when an object on the stage makes contact with the sensor which acts as the signal to stop moving the stage. As I said, I'm new to the whole threading concept, so I'm not even sure if I'm designing this correctly, if I should use backgroundworker or the Thread class, how to ensure thread-safety, etc.
I"m writing the program in C# using Windows Forms and decided to use two background worker controls in Visual Studio/.NET Framework, one to monitor the force sensor reading and the other to control the stage moving up and down.
It has to be fast and accurate; the stage needs to stop moving the moment a pre-defined sensor value is detected. I have two static classes, a ZStageControl static class and a ForceSensorControl static class.
ForceSensorControl has a method called UpdateSensor() which returns the current value of the force sensor, and the ZStageControl has a private static bool variable, forceDetected (set to false by default), a public property ForceDetected that gets/sets this variable, and a method called GoodToMove. GoodToMove is defined as follows:
public static bool GoodToMove()
{
if (forceDetected == true ||
Position() < MinHeight ||
Position() > MaxHeight)
return false;
else
return true;
}
My sensor update backgroundworker dowork code is defined as follows:
private void sensorUpdateBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
double currentForceValue;
while (ForceSensorControl.IsConnected() == true)
{
if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}
else
{
try
{
currentForceValue = Double.Parse(ForceSensorControl.UpdateSensor());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
disconnectForceSensor();
return;
}
if (currentForceValue >= forceSensorStopValue)
{
if (this.ForceDetected != null)
{
this.ForceDetected(this,
new ForceDetectedEventArgs(ZStageControl.Position(), currentForceValue));
}
}
else if (ZStageControl.ForceDetected == true)
ZStageControl.ForceDetected = false;
forceSensorValueLabel.Text = currentForceValue.ToString() + "g";
}
}
}
So as long as the sensor remains connected, continuously loop and update the force sensor vlaue. If it detects the proper force value, it fires the ForceDetected event. If it reads the value as less than the force sensor stop value, but ForceDetected is still set to true, it simply sets to false (since when the force is detected, it will stop the stage from moving and then return it to its default position, so it should reset the force detected variable).
The event code is defined as follows:
public void FormMain_ForceDetected(object sender, ForceDetectedEventArgs e)
{
if (ZStageControl.ForceDetected == true)
return;
ZStageControl.ForceDetected = true;
feedbackRichTextBox.Text += "\nForce Detected at position " +
e.ForceDetectedPosition.ToString() + " with a value of " +
e.ForceDetectedValue.ToString() + "\n";
ScrollToEndOfFeedbackBox(feedbackRichTextBox);
soundPlayer.Play();
}
The thread to move the Z-Stage up or down is defined as follows:
private void zStageMoveBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
ZStageMoveDirections direction = (ZStageMoveDirections)e.Argument;
while (ZStageControl.IsConnected == true)
{
if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}
else
{
System.Threading.Thread.Sleep(zStageSpeed);
if (direction == ZStageMoveDirections.Down)
{
ZStageControl.MoveDown(true);
}
if (direction == ZStageMoveDirections.Up)
{
ZStageControl.MoveUp(true);
}
zStagePositionUpdateLabel.Text = ZStageControl.Position().ToString();
}
}
}
The code that calls the DoWork event for the Z-Stage move is controlled by an if statement that checks if ZStageControl.GoodToMove() is true. So while GoodToMove() returns true, the Z-Stage thread can fire.
The issue I'm having is that I'm not sure if I'm designing this right, if I'm using the backgroundworker properly, and I know my variables are not thread-safe because at certain points GoodToMove() returns true and other times it returns false, even though there is clearly no force being detected. It seems to have a mind of its own. I just know nothing about thread-safety. Should I simply use the THread class instead of the background worker, is there a certain way to ensure the forceDetected variable/GoodToMove() method operates properly across these threads?
I think your approach is inherently flawed.
You seem to be designing a system with a constantly looping monitor and then a check of that monitor when you want to execute a "move."
This is inherently problematic because you've created a race condition between your safety check operation and your move operation.
Consider this:
1 // check if move is ok -- PASS
(async) // move suddenly becomes not ok!
2 // move is executed
Instead you should think about this problem as entirely synchronous (as validation checks and executions should be entirely atomic). Whenever a move is requested you should check if it's permitted and then decide whether or not to execute.
1 // exclusive lock
2 // check if move is ok -- PASS
3 // execute move
4 // release lock
Background:
In my winforms form, I have a Checked ListView and a "master" checkbox called checkBoxAll.
The behaviour of the master is as follows:
If the master is checked or unchecked, all ListViewItems must change accordingly.
If the user unchecks a ListViewItem, the master must change accordingly.
If the user checks a ListViewItem, and all other ListViewItems are checked aswell, the master must change accordingly.
I have written the following code to mimic this behaviour:
private bool byProgram = false; //Flag to determine the caller of the code. True for program, false for user.
private void checkBoxAll_CheckedChanged(object sender, EventArgs e)
{
//Check if the user raised this event.
if (!byProgram)
{
//Event was raised by user!
//If checkBoxAll is checked, all listviewitems must be checked too and vice versa.
//Check if there are any items to (un)check.
if (myListView.Items.Count > 0)
{
byProgram = true; //Raise flag.
//(Un)check every item.
foreach (ListViewItem lvi in myListView.Items)
{
lvi.Checked = checkBoxAll.Checked;
}
byProgram = false; //Lower flag.
}
}
}
private void myListView_ItemChecked(object sender, ItemCheckedEventArgs e)
{
//Get the appropiate ListView that raised this event
var listView = sender as ListView;
//Check if the user raised this event.
if (!byProgram)
{
//Event was raised by user!
//If all items are checked, set checkBoxAll checked, else: uncheck him!
bool allChecked = true; //This boolean will be used to set the value of checkBoxAll
//This event was raised by an ListViewItem so we don't have to check if any exist.
//Check all items untill one is not checked.
foreach (ListViewItem lvi in listView.Items)
{
allChecked = lvi.Checked;
if (!allChecked) break;
}
byProgram = true; //Raise flag.
//Set the checkBoxAll according to the value determined for allChecked.
checkBoxAll.Checked = allChecked;
byProgram = false; //Lower flag.
}
}
In this example, I use a flag (byProgram) to make sure an event was caused by the user or not, thereby preventing an infinite loop (one event can fire another, which can fire the first one again etc. etc.). IMHO, this is a hacky solution.
I searched around but I couldn't find a MSDN documented method to determine if an User Control Event was directly fired thanks to the user. Which strikes me as odd (again, IMHO).
I know that the FormClosingEventArgs has a field which we can use to determine if the user is closing the form or not. But as far as I know, that is the only EventArg that provides this kind of functionality...
So in summary:
Is there a way (other than my example) to determine if an event was fired directly by the user?
Please note: I don't mean the sender of an event! It won't matter if I code someCheckBox.Checked = true; or manually set someCheckBox, the sender of the event will always be someCheckBox. I want to find out if it is possible to determine whether it was through the user (click) or by the program (.Checked = true).
Aaand also: 30% of the time it took to write this question was to formulate the question and the title correctly. Still not sure if it is a 100% clear so please edit if you think you can do better :)
No, there's no practical way to determine whether the change came from GUI or was done by program (in fact, you could analyze the callstack - but that's not recommended because it's very slow and error-prone).
BTW, there's one other thing you could do instead of setting byProgram. You could remove and add the event handler prior or after, respectively, change your controls:
checkBoxAll.CheckedChanged -= checkBoxAll_CheckedChanged;
// do something
checkBoxAll.CheckedChanged += checkBoxAll_CheckedChanged;
Instead of using the changed event, you could use the clicked event to cascade the change through to the relevant controls. This would be in response to a user click, and not the value being changed programatically.
This is something I come across quite a lot and what I tend to try do is not split it between user interaction vs program interaction - I use more generic code i.e. the UI is being updated and doesn't require any events to be handled. I usually package this up through BeginUpdate/EndUpdate methods e.g.
private int updates = 0;
public bool Updating { get { return updates > 0; } }
public void BeginUpdate()
{
updates++;
}
public void EndUpdate()
{
updates--;
}
public void IndividualCheckBoxChanged(...)
{
if (!Updating)
{
// run code
}
}
public void CheckAllChanged(...)
{
BeginUpdate();
try
{
// run code
}
finally
{
EndUpdate();
}
}
Thanks in advance for your help. I'm not a programmer or student asking for homework help, just a technician helping out with work projects.
This one should be easy, but I've brain-farted and I need your help. I've written a GUI control program in C#, but I'll write examples in semi-pseudocode for clarity. I have two boolean control variables and List of void doSomething() methods.
bool ready = true;
bool willLoop = true;
runButton.Click(...) { ...
willLoop is set true/false by a toggle button. I want runButton.Click(...) to iterate through the List of doSomethings(), each of which set ready to false and then back to true when they have finished executing. When ready is set back to true, the next Item in the list will execute and set ready to false and back to true when it is finished. If willLoop is true then the program should iterate though the List over and over again, executing each item. If somebody presses the toggle button and sets willLoop to true or false while the program is executing, I need the program to finish iterating through the list and then either stop(break?) if willLoop is false after the last Item is executed, or iterate again if willLoop is true. All the threading/realtime stuff is handled automatically, I just need a (nested?) looping structure that will use the control variables to do what I need.
I'm only interested in the looping/iterating portion of the code so pseudocode will be fine. The real-world application is controlling an external device with a serial port - each item in the list is a method which sends a command to the device and sets ready to false. When the device finishes moving, it sends a string back and that listener sets ready back to true.
Thanks again for your help.
the structure you need is something like
runButton.Click(...) {
ready = true
while (willLoop)
for each item in list
ready = false
process item
wait until (ready == true)
}
I'm not practical with C# but I guess that Click callback is no asynchronous so as soon as you click the button everything will stop and you won't be able toggle it again. Did you already take care of manage such multithreading issues?
Here is my though on the matter :
public partial class MyFormClass : Form
{
private bool ready = true;
private object readyLock = new object();
private bool willLoop = true;
List<Action> myFunctionList = new List<Action>();
private void button1_Click(object sender, EventArgs e)
{
Queue<Action> remainingActions = new Queue<Action>();
do
{
remainingActions = new Queue<Action>(myFunctionList);
while (remainingActions.Count > 0)
{
lock (readyLock)
{
// In case someone was already in the while,
// but took the last item in the queue.
if (remainingActions.Count == 0) break;
ready = false;
Action currentAction = remainingActions.Dequeue();
currentAction();
ready = true;
}
}
} while (willLoop);
}
}
You'll understand that you can substitute "myFunctionList" by whatever mean you want your function list to be stored.
I have a TextBox with a TextChanged event wired up. In the end it is making a query to a SQL database, so I want to limit the number of queries.
I only want to make the query if the user hasn't pressed a key in say .. 300 milliseconds or so. If for some reason the previous query is still executing, I would need to cancel that, and then issue a new query.
Create a System.Windows.Forms.Timer and reset it (e.g. stop then start it) after every keypress. If the timer event is triggered, disable the timer.
Use the Reactive Framework to trigger on a sequence of events. I'm not sure exactly how this would work, but you can read up on it here (Reactive Extensions for .NET) and see if it will fulfill your needs. There are a bunch of examples here too: Examples. The "Throttling" example may be what you're looking for.
1) Create a timer.
2) Create a handler for the Tick event of your timer. On each tick, check to see if enough idle time has elapsed, and if it has, STOP the timer and execute the query.
3) Whenever a keypress occurs on that textbox, RESTART the timer.
Add a second actionlistener that gets called whenever the user presses any key and when it gets called save the current time to a global variable. Then whenver your TextChanged event gets called it checks to see the time difference between the global variable and the current time.
If the difference is less than 300 milliseconds then start a timer to execute the query after 300 milliseconds. Then if the user presses another key it resets the timer first.
Thanks to #Brian's idea and this answer , I came up with my own version of using a timer to handle this issue. This worked fine for me. I hope it helps the others as well:
private Timer _tmrDelaySearch;
private const int DelayedTextChangedTimeout = 500;
private void txtSearch_TextChanged(object sender, EventArgs e)
{
if (_tmrDelaySearch != null)
_tmrDelaySearch.Stop();
if (_tmrDelaySearch == null)
{
_tmrDelaySearch = new Timer();
_tmrDelaySearch.Tick += _tmrDelaySearch_Tick;
_tmrDelaySearch.Interval = DelayedTextChangedTimeout;
}
_tmrDelaySearch.Start();
}
void _tmrDelaySearch_Tick(object sender, EventArgs e)
{
if (stcList.SelectedTab == stiTabSearch) return;
string word = string.IsNullOrEmpty(txtSearch.Text.Trim()) ? null : txtSearch.Text.Trim();
if (stcList.SelectedTab == stiTabNote)
FillDataGridNote(word);
else
{
DataGridView dgvGridView = stcList.SelectedTab == stiTabWord ? dgvWord : dgvEvent;
int idType = stcList.SelectedTab == stiTabWord ? 1 : 2;
FillDataGrid(idType, word, dgvGridView);
}
if (_tmrDelaySearch != null)
_tmrDelaySearch.Stop();
}