How to make the cycle, running up for 30 seconds? After 30 seconds, the cycle must be repeated with changing one variable(i++).
P.S. I've never used a timer and I need a detailed solution.
the microsoft example seems pretty good. https://msdn.microsoft.com/en-us/library/system.threading.timer%28v=vs.110%29.aspx
here is a cut down version, ive set it for 3 sec rather than 30 so you can verify the result
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
public static void Main(string[] args)
{
example e = new example();
e.timer();
Console.Read();
}
public class example
{
public volatile int i;
public void timer()
{
TimerCallback tcb = callback;
Timer t = new Timer(tcb, i, 3000, 3000);
}
public void callback(object state)
{
Console.WriteLine(this.i);
this.i++;
}
}
}
}
Related
I created two projects(Proj_1,Proj_2), Proj_1 contains Proj_1_Program.cs and ProjectOneClass.cs, Proj_2 contains Proj_2_Program.cs,and I need OnInformed trigger both Informed1 and Informed2 this is how I got so far:
//Proj_1_Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CrossProjectEvent
{
class Proj_1_Program
{
static void Main(string[] args)
{
ProjectOneClass obj1 = new ProjectOneClass();
obj1.Inform += new EventHandler(Informed1);
obj1.InformNow();
Console.ReadLine();
}
private static void Informed1(object sender, EventArgs e)
{
Console.WriteLine("Informed1");
}
}
}
//ProjectOneClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CrossProjectEvent
{
public class ProjectOneClass
{
public event EventHandler Inform;
public void InformNow()
{
OnInformed(new EventArgs());
}
private void OnInformed(EventArgs eventArgs) // I want this method both trigger Informed1 and Informed2
{
if (Inform != null)
{
Inform(this, eventArgs);
}
}
}
}
//Proj_2_Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CrossProjectEvent;
namespace Proj_2
{
public class ProjectTwoClass
{
public ProjectOneClass obj_proj_1;
public ProjectTwoClass()
{
obj_proj_1 = new ProjectOneClass();
obj_proj_1.Inform += new EventHandler(Informed2);
}
private static void Informed2(object sender, EventArgs e)
{
Console.WriteLine("Informed2");
}
}
class Project2
{
static void Main(string[] args)
{
}
}
}
But it seems like only Informed1 being triggered, so how to fix this? thanks!
This is a typical problem requiring inter-process communication. There are a billion different techniques and approaches possible for it.
One solution would be to use Remoting using named pipes (Sample), but also TCP and NetSockets are possible. This might be one of the simplest solutions.
If you are building a larger application requiring a lot of inter-process communication, the actor model, especially the AKKA.NET libary, would be worth mentioning.
But these are just a few of the options you have.
I am printing out the sequence numbers and it jumps by 2 even though I am incrementing it by only 1.
I have a object TradeSequenceNo with a static variable.
I am assuming that a1 and b2 will have values 1,2 but I see that it has values 2,4. how can I make sure that I can get only increments by 1 for everytime I call TradeSequenceNo.NextSequanceNo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class TradeSequenceNo
{
private static int sequenceno;
public string id
{
get
{
return "NextTradeID";
}
}
public static int NextSequanceNo
{
get
{
sequenceno++;
return sequenceno;
}
}
}
public class Program
{
static void Main(string[] args)
{
int a1 = TradeSequenceNo.NextSequanceNo;
int b2 = TradeSequenceNo.NextSequanceNo;
}
}
}
The behaviour you posted is because of the debugger. Every time you check for property value in debugger it will be called resulting in increment of the value. Thus if you visit the same value multiple times in debugger you will get higher number depending upon how many times you checked the variable value. Just execute the following code (without debugging) and you will realize that code is correct and is behaving correctly:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class TradeSequenceNo
{
private static int sequenceno;
public string id
{
get
{
return "NextTradeID";
}
}
public static int NextSequanceNo
{
get
{
sequenceno++;
return sequenceno;
}
}
}
public class Program
{
static void Main(string[] args)
{
int a1 = TradeSequenceNo.NextSequanceNo;
int b2 = TradeSequenceNo.NextSequanceNo;
System.Console.WriteLine(a1);
System.Console.WriteLine(b2);
System.Console.ReadKey();
}
}
}
I can detect the network connection change event while running a C# code, how would I register an exe when Windows detects this event. What all details would I need. Below is how I am using this :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.NetworkInformation;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
testing t = new testing();
Console.Read();
}
}
public class testing{
public testing()
{
NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
}
void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
if (e.IsAvailable)
{
Console.WriteLine("network is available");
}
}
}
}
What you could maybe do is in your method that is triggered to start a new process and execute your exe
I want to create a method which makes my application wait X number of seconds, then continues on down a line of scripts. For example, this is the code that I have so far, after reading many similar help topics:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
methods.WriteTextToScreen(label1, "Hello!");
methods.sleepFor(1);
methods.WriteTextToScreen(label1, "Welcome!");
methods.sleepFor(1);
methods.WriteTextToScreen(label1, "Allo!");
}
public class methods
{
public static int timeSlept;
public static void WriteTextToScreen(Label LabelName, string text)
{
LabelName.Text = text;
}
public static void sleepFor(int seconds)
{
timeSlept = 0;
System.Timers.Timer newTimer = new System.Timers.Timer();
newTimer.Interval = 1000;
newTimer.AutoReset = true;
newTimer.Elapsed += new System.Timers.ElapsedEventHandler(newTimer_Elapsed);
newTimer.Start();
while (timeSlept < seconds)
{
Application.DoEvents();
}
Application.DoEvents();
}
public static void newTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
timeSlept = IncreaseTimerValues(ref timeSlept);
Application.DoEvents();
}
public static int IncreaseTimerValues(ref int x)
{
int returnThis = x + 1;
return returnThis;
}
}
}
}
What I want to do is have my program do the methods.WriteTextToScreen(label1, "Hello!")
then wait for 1 second, then continue on in the same fashion. The problem is that the Form I'm displaying the text on doesn't show up at all until it has written "Allo!" onto the screen, so the first time it appears it already says that. Am I doing something wrong, or is there just no way to do this?
The form doesn't show until it has been constructed i.e. all the code in Form1 is run. See here for info on form constructors: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.form.aspx
To fix your problem you could move the writeTextToScreen and sleep code into the forms on load method. See http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onload.aspx
This program is writing numbers from 1 to 5000 in thread, but main form freezes anyway.
Where is an error? Thanks in advance.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int how, current;
bool job;
Object lockobj = new Object();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Started!");
how = 5000;
current = 0;
job = true;
Thread worker = new Thread(Go);
worker.Name = "1";
worker.Start();
}
private void Go()
{
while (job)
{
if (current < how)
{
lock (lockobj)
{
current++;
}
log(string.Format("Thread #{0}: {1}", Thread.CurrentThread.Name, current));
}
else
{
job = false;
}
}
}
private void log(string text)
{
Action A = new Action(() =>
{
richTextBox1.AppendText(text + System.Environment.NewLine);
});
if (richTextBox1.InvokeRequired)
this.BeginInvoke(A);
else A();
}
}
}
Because most of your work will be spent in
if (richTextBox1.InvokeRequired)
this.BeginInvoke(A);
and while you invoke the form it is locked.
Do some real work, like Thread.Sleep(1000); :-) , instead of current++; and your form will be response between the updates.
It freezes because you are rendering on the textbox very quickly and the GUI doesn't have time to keep in sync. Remember that this rendering happens on the main GUI thread and by calling BeginInvoke to update the textbox so rapidly actually consumes all the resources of this main GUI thread. Try lowering the frequency at which you are logging to avoid this behavior.