I have a problem with threading. I want to have a
processThread
that I can reuse as many times as possible and also gives the ability of stopping an already running thread, dispose the running thread and start a new one in the same thread!
I have tried alot with just Thread and Task but can't get it work as I want.
The reason I want this is to simulate a RealTime calculation from a textbox field, there the thread will be called on KeyUp. And when the user continues to type in the same textbox the Thread that was called from the KeyUp should be restarted. This is connected to a special validation which includes some characters and therefore the process has to be restarted/aborted.
In this method I want to use Thread.Sleep(1500) just to make sure that the user has stopped typing.
The code is as following and this little code gives the error shown in the bottom:
if(processThread.IsAlive)
{
processThread.Abort();
}
Code:
private Thread processThread;
private void incomeTBX1FP_KeyUp(object sender, KeyEventArgs e)
{
bool processSucceed = false;
if(processThread.IsAlive)
{
processThread.Abort();
}
Thread.Sleep(1500);
processThread = new Thread((ThreadStart)delegate
{
ProcessValidationAndMemory(incomeTBX1FP.Text, income1LblFP.Text, 501, 1, out processSucceed);
if (!processSucceed)
{
MessageBox.Show(incomeTBX1FP.Text + "innehåller ett felaktigt värde", "Ett fel har uppstått", MessageBoxButtons.OK, MessageBoxIcon.Error);
incomeTBX1FP.Focus();
incomeTBX1FP.SelectAll();
}
});
processThread.Name = "InputThread";
processThread.Start();
}
private void ProcessValidationAndMemory(string inputString, string inputLabel, short labelIndex, short incomeIndex, out bool processSucceed)
{
double moneyValue;
if (InputUtility.GetIncome(out moneyValue, inputString))
{
incomeArray[incomeIndex] = moneyValue;
allLabels[labelIndex] = inputLabel;
MessageBox.Show(incomeArray[incomeIndex].ToString() + " kr " + allLabels[labelIndex].ToString(), "TEST", MessageBoxButtons.OK);
processSucceed = true;
}
else
{
processSucceed = false;
}
}
Error Message:
System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.
Source=My Economy Lite
StackTrace:
at My_Economy_Lite.MainWindows.incomeTBX1FP_KeyUp(Object sender, KeyEventArgs e) in C:\Users\Michael\documents\visual studio 2010\Projects\My Economy Lite\My Economy Lite\MainWindows.cs:line 39
at System.Windows.Forms.Control.OnKeyUp(KeyEventArgs e)
at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
at System.Windows.Forms.Control.ProcessKeyMessage(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
at System.Windows.Forms.TextBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at My_Economy_Lite.Program.Main() in C:\Users\Michael\documents\visual studio 2010\Projects\My Economy Lite\My Economy Lite\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: