Hello Everyone! i read this article
here and it had an example of how to do operations on a separate thread using a class, it also had an event where when the operation on the new thread completed it would report that the operation was complete to the winform(parent thread) and the winform would print that the operation was completed to the UI. But the problem is that this was in VB.NET, so i attempted to convert this class in to C# but i had no success. so my question is how do i create a class that spawns off a thread that runs a task that i specify, i also want to know how i would make that newly spawned thread report: A. Its current progress in the current operation, B. when it completes(when the progress integer reaches 100) it fires an event that tells the winform to run a method that tells the user that the operation was completed. here is what i have for the thread class so far:
using System;
using System.Speech.Synthesis;
using System.Threading;
public class ManualThread
{
public delegate void CountCompleted();
EventArgs e = new EventArgs();
public event CountCompleted CountCompletedEvent;
public Thread Thread1;
#region Properties
public string TextToSpeak { get; set; }
#endregion
public ManualThread()
{
this.CountCompletedEvent += new CountCompleted(this.CountCompletedEventFiredMethod);
}
public void StartCounting()
{
Thread1 = new System.Threading.Thread(this.Count);
Thread1.IsBackground = true;
Thread1.Name = "Thread1";
Thread1.Start();
}
private void Count()
{
SpeechSynthesizer synth = new SpeechSynthesizer();
synth.SetOutputToDefaultAudioDevice();
synth.SelectVoiceByHints(VoiceGender.Female);
synth.Speak(TextToSpeak);
CountCompletedEvent();
}
private void CountCompletedEventFiredMethod()
{
}
}
then at the winform class level i declare a new instance like this:
ManualThread ThreadOperation = new ManualThread();
then when ever i want to callit i just do this:
ThreadOperation.TextToSpeak = TB.Text;
ThreadOperation.StartCounting();
now the operation runs fine but it can't actually report that the operation has completed to the winform(this is due to the fact that the
CountCompletedEvent(this, e);
gives me a null reference exception error, and therefor doesn't fire the event. therefor i would like to ask 2 things:
1. how would i fix this error. 2. if i can't fix this error how would i make a class that can spawn a new thread that runs an operation, and then fires an event when the operation is completed.
and yes i have read many articles here on codeproject talking about how to do mutlithreading on console and winform apps. but none of them i understood, but the article i read(linked above) i understood but as i said it was done in VB.NET
--UPDATE--
oh and also i am not very experienced with delegates or events(i think delegates have something to do with events) so please excuse my lack of understanding in this area.
NOTE: The class "ManualThread" is not mine but i converted from VB into C#, and it was
taken from the article linked above.
--UPDATE2--
i have figured out how to add an event handler to the "ManualThread" class, i did it like this(i had to add a constructor):
public ManualThread()
{
this.CountCompletedEvent += new CountCompleted(this.Test);
}
but now all it does is fire the event and... and... nothing.
i am successfully firing the event i just don't know how to get the winform class to here the firing of the event so any help on that please?
thank you for all of your help in advance,
MasterCodeon