0

I've a Windows Form with a ListView control. I have another class "job" which gets instantiated and executed on a separate thread from this form after it is loaded. I want to update ListView control on this Form when some action is being performed by the class.

Code of the Job Class is as under: In the DoJob function I want to update some statuses in the listview control on another windows form

 class ConnectToDatabase : Job
{
    /// <summary>
    /// Counter used to count the number of times this job has been
    /// executed.
    /// </summary>
    private int counter = 0;

    public ConnectToDatabase()
    {
    }

    /// <summary>
    /// Get the Job Name, which reflects the class name.
    /// </summary>
    /// <returns>The class Name.</returns>
    public override string GetName()
    {
        return GetType().Name;
    }

    /// <summary>
    /// Execute the Job itself. Just print a message.
    /// </summary>
    public override void DoJob()
    {
        Console.WriteLine(string.Format("This is the execution number \"{0}\" of the Job \"{1}\".", counter.ToString(), GetName()));
        counter++;
    }

    /// <summary>
    /// Determines this job is repeatable.
    /// </summary>
    /// <returns>Returns true because this job is repeatable.</returns>
    public override bool IsRepeatable()
    {
        return true;
    }

    /// <summary>
    /// Determines that this job is to be executed again after
    /// 1 sec.
    /// </summary>
    /// <returns>1 sec, which is the interval this job is to be
    /// executed repeatadly.</returns>
    public override int GetRepetitionIntervalTime()
    {
        return 10000;
    }
}

Currently I solved this issue by making the listview control static in the designer class of the form and accessed it from the class but this approach is not a good practice as designer class is the property of the IDE. What are the best practices to achieve this functionality

DerStarkeBaer
  • 669
  • 8
  • 28
Adnan Qureshi
  • 546
  • 1
  • 6
  • 16
  • 1
    Could you please tell what problem you have encountered? – Alsein Sep 17 '19 at 06:50
  • just edited my question. Currently I solved this issue by making the listview control static in the designer class of the form and accessed it from the class but this approach is not a good practice as designer class is the property of the IDE. What are the best practices to achieve this functionality – Adnan Qureshi Sep 17 '19 at 06:59
  • [Another](https://www.google.com/search?q=pass+data+between+classes+c%23+site:stackoverflow.com) disguised "how to pass data between classes/forms" question. Your best bet to get "best practices" answer is to post current solution and to ask for [codereview](https://codereview.stackexchange.com/), if you don't have code yet, well, that's about [software engineering](https://softwareengineering.stackexchange.com/). – Sinatr Sep 17 '19 at 07:11
  • One more thing. I'm loading Windows Form first then running the instance of class. How can I get that loaded form instance in the class – Adnan Qureshi Sep 17 '19 at 07:20
  • Possible duplicate of https://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the – mustafabar Sep 17 '19 at 09:05
  • No the context is totally different – Adnan Qureshi Sep 17 '19 at 09:07

1 Answers1

0

Ok I found one more solution to my problem: Following code I've placed in the DoJob() function of the class and used Application.OpenForms["FormName"] to access the current instance of the form and then called its public function to set the listview control in the form

public void DoJob()
    {
     FormWorkFlow frm = (FormWorkFlow)Application.OpenForms["FormWorkFlow"];
      if (frm.InvokeRequired)
         {
            frm.BeginInvoke(new Action(() =>
            {
                //This is public function in form that sets the listview control text
                frm.SetListViewEventItems("This call is from the class", "it works"); 
            }));
         }
      else
         {
         }
    }
Adnan Qureshi
  • 546
  • 1
  • 6
  • 16