I must implement an asynchronous communication between client and server, I made a test application that does it through delegates (where the client subscribes to server’s instantiated delegate, that invokes every x seconds). Server and client are running on the same solution, and now I want to put the server on a VM and keep the communication. Is it doable through Delegates? If so, how?
Delegate:
public delegate void ClientStartedEventHandler(String s);
Client:
private void OnStartClick(object sender, EventArgs e)
{
Server.ClientStarted += ClientStarted;
Server.start();
}
private void ClientStarted(String s)
{
MessageBox.Show(s);
}
Server:
public static class Server
{
public static event ClientStartedEventHandler ClientStarted;
public static void start()
{
Timer myTimer = new System.Timers.Timer();
myTimer.Elapsed += new ElapsedEventHandler(exe);
myTimer.Interval = 2000;
myTimer.Enabled = true;
}
private static void exe(object source, ElapsedEventArgs e)
{
String strNow = DateTime.Now.ToString();
if (ClientStarted != null)
{
ClientStarted.Invoke(strNow);
}
}
}
Related
(As I'm new to WCF)
I want to add Quartz in Windows Communication Foundation (WCF) project.
And I also wants to know that which file or method execute first after run the application.
Instead of using Quartz, I used C# Timer.
The Timer class in C# represents a Timer control that executes a code block at a specified interval of time repeatedly.
//Timer Class
public class FileJob
{
private System.Timers.Timer ProcessTimer;
public void Start()
{
try
{
ProcessTimer = new System.Timers.Timer();
ProcessTimer.AutoReset = true;
ProcessTimer.Elapsed += new System.Timers.ElapsedEventHandler(ProcessTimer_Elapsed);
ProcessTimer.Interval = 300000; //5 minutes
ProcessTimer.Start();
}
catch (Exception ex)
{ }
}
private void ProcessTimer_Elapsed(object sender, EventArgs e)
{
UploadFile();
}
}
Global.asax.cs
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
FileJob obj = new FileJob();
obj.Start();
}
}
My question is how do you update your ui, with data from serialport, while still updating other ui components? I have tried using a background worker, but it seems to block the ui, while the data is streaming in.
Image of form:
My code is:
public partial class Form1 : Form
{
static bool _continue;
static SerialPort _serialPort;
BackgroundWorker dataWorker;
string message;
public delegate void UpdateListboxData();
private void buttonPortConnect_Click(object sender, EventArgs e)
{
// Set the read/write timeouts
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;
_serialPort.Open();
_continue = true;
dataWorker = new BackgroundWorker();
dataWorker.RunWorkerAsync();
dataWorker.DoWork += StartDataWork;
}
private void StartDataWork(object sender, DoWorkEventArgs e)
{
Delegate del = new UpdateListboxData(DataRead);
this.Invoke(del);
}
private void DataRead()
{
while (_continue)
{
try
{
message = _serialPort.ReadLine();
}
catch (TimeoutException) { }
}
}
}
you have to use Serial Port DataReceived Event and Delegate
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
if (TextBox.InvokeRequired)
TextBox.Invoke(new myDelegate(updateTextBox));
}
public delegate void myDelegate();
public void updateTextBox()
{
int iBytesToRead = serialPort1.BytesToRead;
//Your Codes For reding From Serial Port Such as this:
char[] ch = new char[?];
serialPort1.Read(ch, 0, ?? ).ToString();
//........
}
The way to update a winforms UI is to use if (control.InvokeRequired) {...} and Invoke as shown in the example here How do I update the GUI from another thread?
You should not read data inside the invoke'd function. This is blocking your UI.
Either read data directly in "DoWork" and invoke the delegate only with data.
Or you might use the DataReceived event of SerialPort.
I have several textboxes in my wpf application. The LostFocus-Event of each textbox starts a backgroundworker to send the data to a connected serial port.
private readonly BackgroundWorker online_mode_send_worker = new BackgroundWorker();
online_mode_send_worker.DoWork += online_mode_send_worker_DoWork;
online_mode_send_worker.RunWorkerCompleted += online_mode_send_worker_RunWorkerCompleted;
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
online_mode_send_worker.RunWorkerAsync(data);
}
private void online_mode_send_worker_DoWork(object sender, DoWorkEventArgs e)
{
List<object> data = (List<object>)e.Argument;
Port.WriteLine(STARTCHARACTER + XMLSET + XML_TAG_START + data[0] + XML_TAG_STOP + data[1] + ENDCHARACTER);
string received = Port.ReadLine();
}
private void online_mode_send_worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//do some things after worker completed
}
At this point, everything is working fine.
But sometimes I have to send two data-points directly after each other and there I have a problem.
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
online_mode_send_worker.RunWorkerAsync(data1);
//wait until backgroundworker has finished
online_mode_send_worker.RunWorkerAsync(data2);
}
The Backgroundworker is still running and I get an exception thrown.
Is it possible to wait after the first online_mode_send_worker.RunWorkerAsync(data) until it has finished and then start the second online_mode_send_worker.RunWorkerAsync(data)?
while(online_mode_send_worker.isBusy); is not working because the main-thread is blocking and the RunWorkerCompleted() is not thrown and so the Backgroundwoker is always busy.
I have found something like this, but Application.DoEvents() is not available in wpf.
while (online_mode_send_worker.IsBusy)
{
Application.DoEvents();
System.Threading.Thread.Sleep(100);
}
Here is a rough idea of what I mentioned in the comments.
public class Messenger {
private readonly BackgroundWorker online_mode_send_worker = new BackgroundWorker();
private readonly ConcurrentQueue<object> messages;
public Messenger() {
messages = new ConcurrentQueue<object>();
online_mode_send_worker.DoWork += online_mode_send_worker_DoWork;
online_mode_send_worker.RunWorkerCompleted += online_mode_send_worker_RunWorkerCompleted;
}
public void SendAsync(object message) {
if (online_mode_send_worker.IsBusy) {
messages.Enqueue(message);
} else {
online_mode_send_worker.RunWorkerAsync(message);
}
}
public Action<object> MessageHandler = delegate { };
private void online_mode_send_worker_DoWork(object sender, DoWorkEventArgs e) {
if (MessageHandler != null)
MessageHandler(e.Argument);
}
private void online_mode_send_worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
object nextMessage = null;
if (messages.Count > 0 && messages.TryDequeue(out nextMessage)) {
online_mode_send_worker.RunWorkerAsync(nextMessage);
}
}
}
You have a queue to hold on to messages that were sent while the background worker was busy and have the worker check the queue for any pending messages when it has completed doing its work.
The messenger can be used like this.
private Messenger messenger = new Messenger();
private void Initialize() { //I would expect this to be in the constructor
messenger.MessageHandler = MessageHandler;
}
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
messenger.SendAsync(data);
}
private void MessageHandler(object message)
{
List<object> data = (List<object>)message;
Port.WriteLine(STARTCHARACTER + XMLSET + XML_TAG_START + data[0] + XML_TAG_STOP + data[1] + ENDCHARACTER);
string received = Port.ReadLine();
}
It seems that I missed the serial stuff. So what you want to do is synchronize your asynchronuouscalls:
private void Button_Click(object sender, RoutedEventArgs e)
{
Task.Run(() => mySerialDevice1.WriteData(data1));
Task.Run(() => mySerialDevice1.WriteData(data2));
}
public class SerialDevice
{
public Port Port { get; set; }
public object _LockWriteData = new object();
public void WriteData(string data)
{
lock(_LockWriteData)
{
Port.WriteLine(data);
}
}
}
also see:
https://msdn.microsoft.com/en-us/library/c5kehkcz.aspx
https://msdn.microsoft.com/en-us/library/de0542zz(v=vs.110).aspx
ORIGINAL ANSWER
You can use Task instead of Backgroundworker.
private void Button_Click(object sender, RoutedEventArgs e)
{
Task.Run(() => OnlineModeSendData(data1));
Task.Run(() => OnlineModeSendData(data2));
}
private void OnlineModeSendData(List<string> data)
{
Port.WriteLine(STARTCHARACTER + XMLSET + XML_TAG_START + data[0]+ XML_TAG_STOP + data[1] + ENDCHARACTER);
string received = Port.ReadLine();
}
I also would like to suggest that you make real objects instead of passing string arrays as arguments.
For Example send BlinkLedRequest:
public class BlinkLedRequest
{
public int LedId{get;set;}
public int DurationInMilliseconds {get;set}
}
and a corresponding method:
public void SendBlinkLed(BlickLedRequest request)
{
....
}
I think your should use RunWorkerCompleted event and add a delegate:
online_mode_send_worker.RunWorkerCompleted += (s, ev) =>
{
if (ev.Error != null)
{
//log Exception
}
//if(conditionToBrake)
// return;
online_mode_send_worker.RunWorkerAsync(data2);
};
online_mode_send_worker.RunWorkerCompleted(data1);
Make sure you put there a condition to avoid infinite loop.
I'd say that if you MUST wait until after the first "job" is done, that what you want is Task.ContinueWith() and change your interface accordingly. The msdn page is good for it IMO, but watch out that you're waiting on the "correct" task object. Hint: it's the return value of ContinueWith() that you should call Wait() on. This is a good pattern to do for launching a Task and then waiting for it later as long as you can keep the Task that is returned so you can wait on it.
For a more generic "I only want one background thread doing things in the order they're added, and I want to wait until they're ALL done and I know when I'm done adding." I would suggest using a BlockingCollection<Action> with only one thread consuming them. An example of how to do that is found in this other answer.
Update:
bw.RunWorkerAsync(data1);
//wait here
bw.RunWorkerAsync(data2);
Is not good aproach, because UI will be blocked on time of waiting. Better:
bw.RunWorkerAsync(new object[] { data1, data2 }); //or new object[] { data1 } if no data2
Original answer:
I advice not to use construction: while (bw.Busy) { ... } (it consumes cpu time), use synchronization objects, for example, ManualResetEvent
BackgroundWorker is great class, but does not support waiting. Just create addition object for waiting:
var bw = new BackgroundWorker();
bw.DoWork += Bw_DoWork;
bw.RunWorkerCompleted += Bw_RunWorkerCompleted;
bool wasError;
ManualResetEvent e = null;
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
if (e != null)
return;
wasError = false;
e = new ManualResetEvent(false); //not signaled
bw.RunWorkerAsync(data1);
e.Wait(); //much better than while(bw.Busy())
if (!wasError)
bw.RunWorkerAsync(data2);
e = null;
}
private void Bw_DoWork(object sender, DoWorkEventArgs e)
{
//background work in another thread
}
private void Bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
//catch exception here
wasError = true;
}
e.Set(); //switch to signaled
}
If you need only call twice you can do this:
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
online_mode_send_worker.RunWorkerAsync(data2);
}
But if you need to queue commands you need rewrite in another way Using Task.
One Task where inside it you will have a for-loop where you will send your data through serial port sequentially.
https://msdn.microsoft.com/pt-br/library/system.threading.tasks.task(v=vs.110).aspx
I have a timer in Global.asax, which calls a method to send current time to all clients through SignalR every 5 seconds:
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapHubs();
var timer = new System.Timers.Timer();
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Interval = 5000;
timer.Enabled = true;
}
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
var context = GlobalHost.ConnectionManager.GetHubContext<EventHub>();
context.Clients.All.Send(DateTime.Now.ToLongTimeString());
}
my Hub class:
public class EventHub: Hub
{
public void Send(string message)
{
Clients.All.broadcastMessage( message);
}
}
Javascript:
$(function () {
var context = $.connection.eventHub;
context.client.broadcastMessage = function (message) {
alert("clock: " + message);
};
$.connection.hub.start();
});
no error, but no thing occurs on running application. what's my wrong?
context.Clients.All.Send(DateTime.Now.ToLongTimeString());
This will fire a method Send on the clients, it will not call
public void Send(string message)
{
Clients.All.broadcastMessage( message);
}
Hello there i am developing Windows Store App.
First of all, here is my code:
public class TickArgs : EventArgs
{
public DateTime Time { get; set; }
}
public class Metronome
{
private DispatcherTimer _timer;
public event TickHandler Tick;
public delegate void TickHandler(Metronome m, TickArgs e);
public Metronome()
{
_timer = new DispatcherTimer();
_timer.Tick += Timer_Tick;
}
private void Timer_Tick(object sender, object e)
{
if (Tick != null)
{
Tick(this, new TickArgs { Time = DateTime.Now });
}
}
public void Start(int bbm)
{
_timer.Stop();
_timer.Interval = TimeSpan.FromSeconds(60 / bbm);
_timer.Start();
}
public void Stop()
{
_timer.Stop();
_timer.Start();
}
}
public class Listener
{
public void Subscribe(Metronome m, MediaElement mmx)
{
m.Tick += (mm, e) => mmx.Play();
}
public void UnSubscribe(Metronome m, MediaElement mmx)
{
m.Tick += (mm, e) => mmx.Stop();
}
}
To start metronome i use these codes:
l.Subscribe(m, mediaelement);
m.Start(120);
This works perfectly fine.
To stop metronome i use these codes:
l.UnSubscribe(m, mediaelement);
m.Stop();
Metronome stops BUT if i try to start again, it just does not start. What should i do?
I would appreciate your helps.
My regards...
Okay, so what you have done is you've subscribed your metronome to two handlers, each happening on the tick timer.
First of all, make a static method in your Listener class as the event handler that you can remove.
private static void TickPlay(object sender, EventArgs e)
{
mmx.Play();
}
Then, in your Subscribe method, just say:
m.Tick += TickPlay;
Lastly, for your Unsubscribe method, say:
m.Tick -= TickPlay;
This way it won't keep going Play/Stop ever tick interval.
I found the solution.
I've just made small changes in start and stop methods:
public void Start(int bbm)
{
_timer.Interval = new TimeSpan(0, 0, 0, 0, 500);
_timer.Start();
}
public void Stop()
{
_timer.Stop();
}
Now it works perfectly fine.
Regards