c# Event handler no overide - c#

I have started a new job, where the last dev left they want a program he started to be finished .
I have got to this problem and have looked at it for half a day.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
logTimer = new System.Windows.Threading.DispatcherTimer();
logTimer.Tick += new EventHandler(logTimer_Tick);
logTimer.Interval = new TimeSpan(0, 0, 0, 1);
logTimer.Start();
txtLogData.Text = Logger.GetLines();
try
{
DataProcessor gaugeProcessor = new DataProcessor(SQLConnectionString);
gaugeProcessors.Add(gaugeProcessor);
grdProcessor.ItemsSource = gaugeProcessors;
List<GaugePort> ports = SQLClient.GetGaugePorts(SQLConnectionString);
foreach(GaugePort port in ports)
{
GaugePortListener newListener = new GaugePortListener(port);
listeners.Add(newListener);
}
grdPorts.ItemsSource = listeners;
}
catch(Exception ex)
{
}
}
I am getting an error on line 4 "No Overload for ' logTimer_Tick' matches delegates 'Event Handler'"
The Function it calls dose exist and looks like this
private void logTimer_Tick(object sender, EventArgs e)
{
txtLogData.Text = Logger.GetLines();
}
I have had a look at the links below but i have drawn a blank
http://www.yoda.arachsys.com/csharp/threads/parameters.shtml
C# method name expected
Any ideas would be great
Thanks in advance
EDIT
Change the wording for the error message "Typo"

Directly use the method:
logTimer.Tick += logTimer_Tick;
This should help, as the compiler does strange things with your EventHandler.
The weird thing is that your code seems to work on my machine - that means the code you posted isn't equal to the code you tried or it's a bug caused by your compiler. Or, as a third possibility, the logtimer isn't a WinForms timer, then I can't reproduce your problem.
In this third case it may be possible that the second parameter isn't an EventArg (even though it'd be strange that it works if you don't use the EventHandler stuff). Then you could try an object as second parameter:
private void logTimer_Tick (object sender, object e)
It seems to be neccessary for Windows phone 8.1 (No overload for 'method' matches delegate 'System.EventHandler').

Related

How do i get the EventHandler(SpeakCompleted) in C# to work? (System.Speech)

Simply put, I want to make a program that counts down (+ voice) but when numbers like 1mil comes this takes longer than 1 sec to pronounce so I would like to find out how I can get the "EventHandler" to run and how I can use it (i do not need code for counting etc. but how to create an EventHandler and where i need to write the Code when its been called)
https://learn.microsoft.com/en-us/dotnet/api/system.speech.synthesis.speechsynthesizer.speakcompleted?view=netframework-4.8
I cant just use Text.Speak("") cuz this leads to desync with the Text printed. I need that callback to start a new Speak and sync it with Text.
Sry... i hate to ask ppl but after 3h i surrender pls help me
SpeechSynthesizer synth = new SpeechSynthesizer();
int counting = 0;
private void TTS() //First trigger
{
synth.SetOutputToDefaultAudioDevice();
synth.SelectVoiceByHints(VoiceGender.Female, VoiceAge.NotSet, 0);
textBox1.Text = "1";
synth.Speak("1");
counting = 1;
synth.SpeakCompleted += synth_SpeechOver;
}
//public event EventHandler<System.Speech.Synthesis.SpeakCompletedEventArgs> SpeakCompleted; deleted
public void synth_SpeechOver(object sender, EventArgs e)
{
synth.SetOutputToDefaultAudioDevice();
synth.SelectVoiceByHints(VoiceGender.Female, VoiceAge.NotSet, 0);
counting++;
synth.Speak(counting.toString());
}
void Form1_SpeakCompleted(object sender, EventArgs e)
{
}
//(Form1_SpeakCompleted is just for testing (doesnt work)
Callback only works for SpeakAsync
https://learn.microsoft.com/en-us/dotnet/api/system.speech.synthesis.speechsynthesizer.speakcompleted?view=netframework-4.8
The SpeechSynthesizer raises the SpeakCompleted event at the completion of any of the SpeakAsync or SpeakSsmlAsync methods.

c# Double Agent command event handler

here I'm talking about Double Agent (a windows7/8 version of MS Agent, the same as office 2007 I suppose).
I'm sorry I'm talking about a full product but i'm really being mad to catch an event( the bundled sample does not help for this)...
In the sample i have a similar handle:
public MsaWithDa()
{
InitializeComponent();
mDaControl = new DoubleAgent.Control.Control ();
mDaControl.Show += new DoubleAgent.Control.ShowEventHandler (mDaControl_Show);
}
and this:
private void mDaControl_Show(string CharacterID, DoubleAgent.Control.VisibilityCauseType Cause)
{
SetDaControlButtons();
}
Now I need to handle a different event (when the user select a command from the menu of the Agent).. and I have this
private void mainAgent_Command(object sender, AgentObjects.IAgentCtlCommand e)
{
mDaControlChar.Play("Wave");
mDaControlChar.Speak("Hello!");
}
It's based on the user manual of the product:
Double Agent sends this event when your application is input-active
and the user chooses a command from the character's pop-up menu, or by
spoken input.
public event CtlCommandEventHandler CtlCommand
I added this to the main form:
mDaControl.Command += new DoubleAgent.Control.Command(mDaControl_Command);
but something is missing and I have to pass the two values to be able to test.
Sorry, I understand this is a stupid question and sure super-basic but this is the first time I need to use Event Handlers in c#
Hope someone should help, thanks a lot
EDIT:
Based on this article: Understanding events and event handlers in C#
I now coded this:
public delegate void MyEventHandler(object sender, AgentObjects.IAgentCtlCommand e);
public event MyEventHandler AgentObjects;
and this:
private void InitializeAgent()
{
mDaControl.Command += new MyEventHandler(HandleSomethingHappened);
}
private void HandleSomethingHappened(object sender, AgentObjects.IAgentCtlCommand e)
{
mDaControlChar.Play("Wave");
mDaControlChar.Speak("Hello!");
}
BUT i have an error here:
new MyEventHandler(HandleSomethingHappened)
Error 1 Cannot implicitly convert type 'XCopyPro.Main.MyEventHandler'
to
'DoubleAgent.Control.CommandEventHandler' C:\Users\Shawn\Documents\Visual
Studio 2013\Projects\XCopyPro\XCopyPro\FormMain.cs 159 37 XCopyPro
You should be able to do a new DoubleAgent.Control.CommandEventHandler instead of a new MyEventHandler. As long as your method has the same signature as the DoubleAgent event handler it should work.
Sorry, i'm a newbie in C#! You should not help me without docs...
I solved myself following the manual...
The code is pretty simple, somewhere this:
mDaControl.Command += new DoubleAgent.Control.CommandEventHandler(mDaControl_Commands);
And this:
private void mDaControl_Commands(DoubleAgent.Control.UserInput e)
{
mDaControlChar.Play("Wave");
mDaControlChar.Speak("Hello!");
}

Issues with using and calling EventHandlers

I'm using Kellerman .NET SFTP Library and I'm having some issues using event handlers
According to the documentation it has the following events:
I'm interested in two of them:
TransferCompleteEvent
FailureEvent
I would like to display a message when the transfer is complete and restart the upload if connection failed.
In my class I have the following:
public static void uploadToSFTP()
{
try
{
SFTP myConnection = new SFTP();
myConnection.EnableLogging();
myConnection.HostAddress = "servername";
myConnection.UserName = "username";
myConnection.Password = "password";
myConnection.CurrentDirectory = "directory";
myConnection.Connect();
//UPLOADING FILE TO SFTP SERVER
myConnection.UploadFileAsync(yesterdaysZipFile, localZipFileName);
while (myConnection.IsBusy == true)
{
//PRINT HOW LONG REMAINING FROM UPLOAD
Console.WriteLine(myConnection.EstimatedTimeRemaining);
}
//declaring an eventhandler
myConnection.TransferCompleteEvent += SFTPCompleted;
myConnection.Disconnect();
myConnection.Dispose();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
And then I have
public static void SFTPCompleted(Object sender, TransferCompletedEventArgs e)
{
Console.WriteLine("Completed");
}
My problem is in this line:
myConnection.TransferCompleteEvent += SFTPCompleted;
When I use the debugger and get to this line it skips and goes to the next line it never goes to
public static void SFTPCompleted(Object sender, TransferCompletedEventArgs e)
{
Console.WriteLine("Completed");
}
What am I doing wrong here?
And in regards the FailureEvent I can't even get it to compile:
myConnection.FailureEvent += TransferFailed;
Here's the event:
public static void TransferFailed(Object sender, SFTP.FailureEventHandler e)
{
Console.WriteLine("failed");
}
I get this compiler error:
Error 1 No overload for 'TransferFailed' matches delegate 'KellermanSoftware.NetSFtpLibrary.SFTP.FailureEventHandler'
This is my first time using this library. Any suggestion would be helpful.
This line:
myConnection.TransferCompleteEvent += SFTPCompleted;
is attaching an event handler.
You are attaching the event handler after calling UploadFileAsync and waiting for it to stop being busy. At this point, the event would have already fired, so you're missing out on hearing about it.
You should attach the event handler as soon as you create the myConnection object.
SFTP myConnection = new SFTP();
myConnection.TransferCompleteEvent += SFTPCompleted;
Your second problem is that you cannot attach the TransferFailed event. That's because you have the wrong parameters in your handler function. My guess is that it should be:
public static void TransferFailed(Object sender, SFTP.FailurEventArgs e)
Have a look at the SFTP.FailureEventHandler declaration. It will tell you what the parameters need to be.

why my WP7 Timer sometimes stops to count?

i have a problem with a application. this application simply shows a number value every second. you can see it as a countdown. the problem is, that this Timer sometimes stop to tick and i dont know why. where is my code:
public MainPage()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}
i start the timer afte the Loaded event:
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
Timer t1 = new Timer(TimerCall);
t1.Change(0, 1000);
}
and here is the method which chanes the text:
private void TimerCall(object state)
{
TextField.Dispatcher.BeginInvoke(delegate
{
TextField.Text = "some text change";
});
}
I dont understand why this sometimes stops
Have a look at this article especially the section on The Tombstone
Next to the fact that the Timer is a local variable instead of a class member, you might be running into the tomb stoning process. The article explains this quite well.

multi thread issue

I'm using a timer to reset a lable I use as a warning box. Basically, if the user does something (more specifically, something goes wrong, ex : He uses a word not recognized by the program), this catches what went wrong early and returns to him what happened so he can change the input.
The reset blanks out the label after 5 seconds to prevent him from seeing something like "please do not use chinese characters" and maybe still thinking an old error is still up. This is what I got reading the invoke (since I hear begininvoke requires an endinvoke, I chose invoke).
private void lblWrn_TextChange(object sender, EventArgs e)
{
Timee = new System.Timers.Timer(5000);
Timee.Elapsed += new ElapsedEventHandler(timerClearWrn);
Timee.Enabled = true;
}
string empty = "";
private void timerClearWrn(object sender, ElapsedEventArgs elapsed)
{
lblWrn.Invoke(new Action<Label>(lblWrn), new object[] { lblWrn, "" });
}
I am not too sure where I am going wrong with this, and looking up examples, cannot figure out which part to change. Can someone explain to me the error or invoke a bit more?
If it's a Windows Forms application, use System.Windows.Forms.Timer, then you don't need Invoke, as the timer callback is executed on the main thread.
Also, don't create a new timer on every text change.
Actually, Control.BeginInvoke does not need an EndInvoke; it is Delegate.BeginInvoke that does.
First, I would also recommend using a Windows.Forms.Timer, since it looks like you are using winforms - that will automatically fire on the UI thread, making all the problems go away - just run the code you want to run in the handler (don't use Invoke etc)
The problem in your example is that the parameters don't match; an Action<> expects a method name (more accurately: a method group) to be invoked, and the parameters in the array must be suitable. Since you don't show the method you plan to invoke, I can't help there - but lblWarn isn't a method (it is a field).
on this line
lblWrn.Invoke(new Action(lblWrn), new object[] { lblWrn, "" });
shouldn't the bold part be a function and not a object?
You have a few options. Option 1 is a little clunky. Options 2 and 3 are better.
Option 1: Continue with general strategy of using Control.Invoke but use code that calls Invoke correctly, disable auto resetting of the timer, and removes the event handler.
private void lblWrn_TextChange(object sender, EventArgs e)
{
var Timee = new System.Timers.Timer(5000);
Timee.Elapsed += this.timerClearWrn;
Timee.AutoReset = false; // Raise the Elapsed event only once
Timee.Enabled = true;
}
private void timerClearWrn(object sender, ElapsedEventArgs elapsed)
{
lblWrn.Invoke(
(MethodInvoker)(()=>
{
lblWrn.Text = "";
}), null);
var Timee = (System.Timers.Timer)sender;
Timee.Elapsed -= this.timerClearWrn;
}
Option 2: Use a System.Windows.Forms.Timer instead of System.Timers.Timer.
Option 3: Use the SynchronizingObject property of System.Timers.Timer. This is my preferred option when timers are to be created and used dynamically from a UI thread.
private void lblWrn_TextChange(object sender, EventArgs e)
{
var Timee = new System.Timers.Timer(5000);
Timee.Elapsed += this.timerClearWrn;
Timee.AutoReset = false; // Raise the Elapsed event only once
Timee.SynchronizingObject = this; // Tell the Timer to raise the Elapsed event on the UI thread
Timee.Enabled = true;
}
private void timerClearWrn(object sender, ElapsedEventArgs elapsed)
{
lblWrn.Text = "";
var Timee = (System.Timers.Timer)sender;
Timee.Elapsed -= this.timerClearWrn;
}

Categories