Click here to Skip to main content
15,891,002 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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()
      {
          // single example resistance value
          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(); // close if button is pressed
                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
.
Posted
Updated 4-Mar-21 4:13am
v7
Comments
[no name] 26-Feb-21 15:06pm    
Your "non-modal" form freezes because of your "looping and sleeping"; the UI is "locked out"; you need a "background worker" / separate thread. This example is important to learn well; most people don't get it or will spout on about "async".

https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.backgroundworker?view=net-5.0
Member 14770221 2-Mar-21 13:32pm    
The background Worker will allow me to do the loop in the background. How do I pause the main program to wait for the result. With the loop in background control of the main program progresses to the next test. I need the results of the form output to be used in the next test. to Progress or Fail the test. I will be repeating this multiple times throughout the test.

1 solution

Instead of using form.ShowDialog(), use form.Show().

HOWEVER, if you start using modeless forms, you have to know about all currently open forms so you can make sure they're closed when the app is closed.

FWIW, if the app you're updating is using ShowDialog now, why would you want to change the behavior?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900