I am new to C# I have been given the task of upgrading code from Visual Studio C++ 6.0 to Visual studio 2017. I have a form that takes an external input from a national instrument box as well as button to close the function. If I use a Modal form I have to use the close button I can not find a way to pass the external input in. If i use the Non-Modal form I can terminate with the external input but the form freezes and is unresponsive thus you can not click the button? How can i Solve this problem?
File Main_Menu.cs from a form app
public void OverloadTripearly()
{
OverloadTripEarly overloadTripEarly = new OverloadTripEarly();
overloadTripEarly.Owner = this;
overloadTripEarly.ShowDialog();
}
File NIDMMPXIeSlot5ConsoleApplication.cs
public static double SingleResistanceMeasurementApp()
{
NIDmm sampleDmmSession = new NIDmm("PXI1Slot5", true, true);
double range = 100;
double resolution = 6.5;
sampleDmmSession.ConfigureMeasurementDigits(DmmMeasurementFunction.TwoWireResistance, range, resolution);
double reading = sampleDmmSession.Measurement.Read();
double DMMValue = reading;
sampleDmmSession.Close();
return reading;
}
OverloadTripEarly.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinFormTitlepageNew
{
public partial class OverloadTripEarly : Form
{
public OverloadTripEarly()
{
InitializeComponent();
}
private void btnOK_Click(object sender, EventArgs e)
{
if (backgroundWorker1.IsBusy)
{
backgroundWorker1.CancelAsync();
}
else
{
this.Close();
this.Dispose();
}
}
private void OverloadTripEarly_Load(object sender, EventArgs e)
{
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
do
{
Thread.Sleep(100);
GlobalVariables.OverloadTest = NIDMMPXIeSlot5ConsoleApplication.SingleResistanceMeasurementApp();
if (double.IsNaN(GlobalVariables.OverloadTest))
{
GlobalVariables.OverloadFlag = true;
this.Close();
e.Cancel = true;
}
if (backgroundWorker1.CancellationPending)
{
e.Cancel = true;
this.Close();
return;
}
} while (double.IsNaN(GlobalVariables.OverloadTest));
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{ this.Close();
this.Dispose();
}
}
private void button1_Click(object sender, EventArgs e)
{
GlobalVariables.OverloadTest = NIDMMPXIeSlot5ConsoleApplication.SingleResistanceMeasurementApp();
if (double.IsNaN(GlobalVariables.OverloadTest))
{
textBox1.Text = "NaN".ToString();
}
else
{ textBox1.Text = GlobalVariables.OverloadTest.ToString(); }
}
}
}
What I have tried:
I have tried using Both Modal and Non-Modal forms show() and show dialog(). So far I can not have both input pieces with functional code. The show() freezes in the loop. The Showdialog() will not take the external input.
I added a background worker Class.
I will have to use a Modal showdialog form. I have added a button and textbox I can detect when the value changes after i click the button. How can i use the background worker to close this form when the value changes I thought i did that with the background worker but it not doing what i want it to
.