1

So I tried to create a new form and reference it...the compiler didn't mind this but it clearly wasn't changing the visibility of my picturebox. this is how I was calling my method found in my form, FROM my c# script.

Form1 updateForm = new Form1();
updateForm.setLights();

It called the method, and seemed like it worked! Until I read a post about instancing forms, and how by creating a "new" instance of Form1, that anything referenced by my updateForm would not change what I would see on my Form1.

So what I need to do is to call the function in setLights() which is in my Form1, and get it to change the visibility of my image on that form, from my C# code. Please see below (i understand the issue of the instancing problem mentioned above, but I left it in so that hopefully it will give better insight into what I am "trying" to do :) ALSO, please keep in mind that setLightCall() is running in a separate thread. Thanks in advance!

This code is also in my main c# script, and is the main function that I use to call my threads

static void Main(string[] args)
    {
        Thread FormThread = new Thread(FormCall);
        FormThread.Start();

        Thread setLightThread = new Thread(setLightCall);
        setLightThread.Start();

        log4net.Config.XmlConfigurator.Configure();
        StartModbusSerialRtuSlave();
    }

This code is in my main C# script

public  void setLightCall(Form1 parent)
    {
        Form1 updateForm = new Form1();
        while(true)
        {
            updateForm.setLights();

        }


    }

The below code is in my form1

public void setLights()
    {
        Input1GreenLight.Visible = false;
    }
Pardon_me
  • 705
  • 2
  • 8
  • 18
  • how are you passing your Form reference to your setLightCall method ? – Mark Hall Aug 20 '14 at 00:07
  • I'm not sure if I'm addressing your question but I am passing it through my main c# script, and **setLightCall** is being called from my **main** method – Pardon_me Aug 20 '14 at 00:10
  • can you show the code that you are using to call the method – Mark Hall Aug 20 '14 at 00:12
  • There are a LOT of things I don't understand in your code. First off, you say "C# script" a couple of times, but you're not talking about a scripting language based on C# are you? If not, it would be less confusing if you said "C# program". – RenniePet Aug 20 '14 at 01:06
  • 1
    Next, you can't start up a WinForms program (that is what this is, isn't it?) by just instantiating a Form in your Main() method. You need to start the Windows message pump by calling Application.Run(). But this is normally something you don't write in a program yourself. If you create a new WinForms project with Visual Studio, then Visual Studio creates all of the boiler-plate code for your automatically. Unless that is not possible for you, then that is the way to go. Even if that is not possible in your project, then it would help if you created a project like that anyway to study. – RenniePet Aug 20 '14 at 01:10
  • 1
    Finally, as you do realize, there are multi-threading limitations when working with WinForms. All code that modifies the WinForms UI controls must run on the UI thread (usually thread zero). So when you need to change a WinForms control from code running on another thread you need to use WinForms Invoke() or BeginInvoke() methods. There's tons of info about how to do this on the Internet. Here's one link to get you started: http://stackoverflow.com/questions/229554/whats-the-difference-between-invoke-and-begininvoke – RenniePet Aug 20 '14 at 01:15
  • @RenniePet My apologies....you are right, I should say my C# program. As for the WinForms/Application.Run I do have those calls in another thread running concurrently (the first call actually in my Main() ), so I've got that covered, and thank you kindly for the link, sorry for the delayed reply as I just got back home from a commute, but I will review it and take notes. Thank you for your insights – Pardon_me Aug 20 '14 at 05:32

1 Answers1

2

Here is an example of what I think you are wanting to try. Note the use of Invoking and delegates to be able to access the PictureBox's Visible method. I had to add the System.Windows.Forms Namespace to the Console Application to be able to access the instance of the Form that was created in the FormThread Method, this is assuming that you only have 1 Form in your FormCollection.

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Windows.Forms;

namespace ConsoleApplication59
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread FormThread = new Thread(FormCall);
            FormThread.Start();

            Thread.Sleep(2000); //Sleep to allow form to be created

            Thread setLightThread = new Thread(setLightCall);
            setLightThread.Start(Application.OpenForms[0]); //We can get by with this because just one form

            Console.ReadLine();

        }

        public static void setLightCall(object parent)
        {
            Form1 updateForm = (Form1)parent;
            while (true)
            {
               updateForm.Invoke(updateForm.setLights, new object[] { false });

            }

        }

        public static void FormCall()
        {
            Application.Run(new Form1());

        }
    }
}

Form1

public partial class Form1 : Form
{
    public delegate void Lights(bool state);
    public Lights setLights;
    public Form1()
    {
        InitializeComponent();
        setLights = new Lights(setLightsDelegate);
    }




    public void setLightsDelegate(bool state)
    {
        Input1GreenLight.Visible = state;
    }

}
Mark Hall
  • 51,582
  • 8
  • 87
  • 106
  • 1
    Thank you very very much for taking the time and offering this wonderful example!! You're a class-A act, and honestly, I have seen snippets of your code in examples relating to what I am trying to do so although I can't check it immediately, I will be sure to give it a go tomorrow. C# is new to me, as I'm used to C/C++, but thanks to people like you I can keep trudging forward and getting my head around this neat language. I will give it a try tomorrow and follow up. – Pardon_me Aug 20 '14 at 05:36
  • 1
    Mark I just implemented your code and it worked flawlessly, I can not thank you enough. I clearly need to do more reading on invoke/delegates... – Pardon_me Aug 20 '14 at 21:59
  • Thank you for the kind comments. I am glad that the code worked for you. – Mark Hall Aug 20 '14 at 22:01