Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
what is another command for 'Goto' ? I am using VSTO to make a ribbon for excel and is doesnt seem to support Goto, and I am trying to create a loop.
Edit: This is the loop i am trying to create :
TimeSpan startTimeSpan = new TimeSpan(0, 0, 0, 20);
TimeSpan timeDecrease = TimeSpan.FromSeconds(1);
private Timer timer = new Timer();
public void Ribbon_Load(Office.IRibbonUI ribbonUI)
{
timer.Interval = 1000;
this.ribbon = ribbonUI;
timer.Tick += timer_Tick;
timer.Start();
}
private void timer_Tick(object sender, EventArgs e)
{
if (startTimeSpan.ToString() != "00:00:00")
{
startTimeSpan = startTimeSpan - timeDecrease;
ribbon.InvalidateControl("timerLabel");
}
else
{
//when timer drop to "00:00:00" then loop to "TimeSpan startTimeSpan = new TimeSpan(0, 0, 0, 20);"
}
}
You probably want to be using a while loop, with the exit condition in the while statement, and use break to quit the loop before the exit condition is reached, or continue to skip the current iteration of the loop and go on to the next.
C# does support goto command, but it isn't for looping. Jump statements are very often not the best solution.
goto - http://msdn.microsoft.com/en-us/library/13940fs2.aspx
If you are trying to loop just use a looping statement.
looping - http://msdn.microsoft.com/en-us/library/ms228598(v=vs.90).aspx
You can find a lot of q&a on excel VSTO.
Whats wrong with something like:
for (int i = selectedRange.Rows.Count; i > 0; i--)
{
---YOUR CODE HERE---
}
OR
foreach (Excel.Range row in rng.Areas[1].Rows)
{
---YOUR CODE HERE---
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am not very good at this, but I have an idea, and I want to ask here if it is possible. So I want to make, so PictureBox grows bigger, grows to some size and then goes smaller again, and continue doing this. I have only figured out how to make, so it goes bigger or smaller, but I can not figure out how to make so it detects specific size, and then do the opposite. The code I use.
Size size = pictureBox.Size;
size.Height--;
size.Width--;
pictureBox.Size = size;
Just an example, since you didn't specify your criterion for enlarging and reducing. Initialize your timer:
// set the interval you prefer
System.Timers.Timer timer = new System.Timers.Timer(500);
timer.Elapsed += OnElapsed;
timer.AutoReset = true;
timer.Enabled = true;
Create a member variable that controls whether the PictureBox must be enlarged or reduced:
private Boolean m_Reducing = false;
And then put everything together in your subscribed timer handler:
private static void OnElapsed(Object sender, ElapsedEventArgs e)
{
Size size = pictureBox.Size;
if (m_Reducing)
{
--size.Height;
--size.Width;
if ((size.Width == minimumWidth) && (size.Height == minimumHeight))
m_Reducing = false;
}
else
{
++size.Height;
++size.Width;
if ((size.Width == maximumWidth) && (size.Height == maximumHeight))
m_Reducing = true;
}
pictureBox.Size = size;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am making a food ordering desktop application , so i want to calculate time between ordering food and delivering food so i added timers to the app and made a start and end buttons , on starting i start a time and put it interval value ,make a variable counter, count and save its value on end button to database , what i want to made is to start a new timer dynamically on new orders and when ending an order stop its timer
i tried inserting 3 timers and made variables c1,c2,c3 and made a loop to start timers on every order if interval!=null , but i didn't know how to stop a specific timer on ending the order
code :
int c1=0;
int c2=0;
int c3=0;
private void button_start_Click(object sender, EventArgs e)
{
if (timer1.Interval == null)
{
timer1.Start();
timer1.Interval = 1000;
}
else if (timer2.Interval == null)
{
timer2.Start();
timer2.Interval = 1000;
}
else if (timer3.Interval == null)
{
timer3.Start();
timer3.Interval = 1000;
}
}
Well as you have not shown us any code, let me assume that you at least have a class to encapsulate order.
public class Order
{
public int OrderNumber{get;set;}
///other properties
}
Now if you add following two properties and a method, your problem is resolved.
public class Order
{
public int OrderNumber{get;set;}
//other properties
public DateTime OrderPlacedOn{get;set;}
public DateTime OrderCompletedOn{get;set;}
public TimeSpan TimeToComplete()
{
if(OrderCompletedOn==DateTime.MinValue)//order not completed yet
return TimeSpan.FromSeconds(0);
return OrderCompletedOn - OrderPlacedOn;
}
}
This saves you from keeping countless timers. You can set values of start and complete on clicking of your buttons.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I wrote a client application where I am using two threads other than the main thread.The Background thread is used to receive data through TCP IP,while after receiving I am calling another thread that will show the received data continuously using a for loop.But after every iteration the for loop needs to wait for 30 seconds.I have used Thread.sleep(30000) but the dont know why its not working,sometimes it wakes up from sleep mode before 30 seconds.I am using dot net framework 4.Please suggest some other alternatives.
for(n=0;n<=m;n++)
{
//show data;
//wait for 30 seconds
}
Timers are not accurate in .NET . You might not get 30000 exactly.
You can using Thread.Sleep(30000); Or await Task.Delay(30000) but you need to mark your method as async;
The difference is that Thread.Sleep block the current thread, while Task.Delay doesn't.
public async Task MyMethod()
{
int n;
int m = 100;
for(n=0; n<=m; n++)
{
//show data;
//wait for 30 seconds
await Task.Delay(30000);
}
}
For .NET 4, you can use this alternative:
public static Task Delay(double milliseconds)
{
var tcs = new TaskCompletionSource<bool>();
System.Timers.Timer timer = new System.Timers.Timer();
timer.Elapsed += (obj, args) =>
{
tcs.TrySetResult(true);
};
timer.Interval = milliseconds;
timer.AutoReset = false;
timer.Start();
return tcs.Task;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How to execute the multiple timers in different time interval using window scheduler....?
Is it possible using this functionality in .net...?
Thanks
You can also use single timer for different time intervals
for example:
private void Form_Load()
{
timer1.Interval = 1000; // 1 second
timer1.Start(); // Start timer, This will raise Tick event after 1 second
OnTick(); // So, call Tick event explicitly when we start timer
}
Int32 counter = 0;
private void timer1_Tick(object sender, EventArgs e)
{
OnTick();
}
private void OnTick()
{
if (counter % 1 == 0)
{
OnOneSecond();//Write your code in this method for one second
}
if (counter % 2 == 0)
{
OnTwoSecond();
}
counter++;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
To put it simply,
I start running my C# program in the morning, and the program should show the user a message at 5:45 PM. How can I do this in C#?
Edit: I asked this question because I thought using a timer is not the best solution (comparing the current time periodically to the time I need to run the task):
private void timerDoWork_Tick(object sender, EventArgs e)
{
if (DateTime.Now >= _timeToDoWork)
{
MessageBox.Show("Time to go home!");
timerDoWork.Enabled = false;
}
}
I asked this question because I thought using a timer is not the best solution (comparing the current time periodically to the time I need to run the task)
Why? why not timer a best solution? IMO timer is the best solution. but not the way you have implemented. Try the following.
private System.Threading.Timer timer;
private void SetUpTimer(TimeSpan alertTime)
{
DateTime current = DateTime.Now;
TimeSpan timeToGo = alertTime - current.TimeOfDay;
if (timeToGo < TimeSpan.Zero)
{
return;//time already passed
}
this.timer = new System.Threading.Timer(x =>
{
this.ShowMessageToUser();
}, null, timeToGo, Timeout.InfiniteTimeSpan);
}
private void ShowMessageToUser()
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(this.ShowMessageToUser));
}
else
{
MessageBox.Show("Your message");
}
}
Use it like this
SetUpTimer(new TimeSpan(17, 45, 00));
You can use Task Scheduler too.
Also there is a Timer class which can help you
You may easily implement your own alarm class. To start with, you may want to check the Alarm class at the end of the MS article.
You can use a Timer to check each minute if DateTime.Now == (the specific time you want)
This is an example of a code with windows forms
public MainWindow()
{
InitializeComponent();
System.Windows.Threading.DispatcherTimer timer_1 = new System.Windows.Threading.DispatcherTimer();
timer_1.Interval = new TimeSpan(0, 1, 0);
timer_1.Tick += new EventHandler(timer_1_Tick);
Form1 alert = new Form1();
}
List<Alarm> alarms = new List<Alarm>();
public struct Alarm
{
public DateTime alarm_time;
public string message;
}
public void timer_1_Tick(object sender, EventArgs e)
{
foreach (Alarm i in alarms) if (DateTime.Now > i.alarm_time) { Form1.Show(); Form1.label1.Text = i.message; }
}