Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to write a thread that will load data into a winform while other processing continues in the background. I have put the definition of the thread class in a separate file in the project - Thread.cs. Here is the code in that file:

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


namespace Thread_Test_Sending_Data_to_Thread_Function
{
    using System.Windows.Forms;

    public class Thread1
    {
        

        private delegate void SetLabelTextDelegate(FormData InputClass);
        public void FormWriteMethod(FormData InputClass)
        {
           

            if (this.InvokeRequired)
            {
                this.BeginInvoke(new SetLabelTextDelegate(FormWriteMethod), new object[] { InputClass });
                return;
            }
            TestLabel1.Text = InputClass.Label1Data.ToString();
            TestLabel2.Text = InputClass.Label2Data.ToString();
        }
    }
}


I know the definition of InvokeRequired is in System.Windows.Forms. The reference works in the main body but when I try to reference InvokeRequired or BeginInvoke in the Thread.cs it isn't recognized even though I have used System.Windows.Forms. None of the using statements in the file are recognized.

What is going on?

Thanks for any help,

Andy Cruce

What I have tried:

I have tried referencing the Invoke methods in the main body and it works fine. Have also tried a similar reference in other small class files with the same result - namely it doesn't look like the using statements are recognized.
Posted
Updated 14-Nov-16 9:01am
Comments
andycruce 12-Nov-16 18:09pm    
I originally didn't have the using System.Windows.Forms statement inside the namespace, just the list of using statements at the top of the file. I put there just to see if it would work. I just removed the single using statement inside the namespace to get back to the original configuration and the result was still the same. The using System.Windows.Forms is not used - VS suggests removing all the using statements as unused - and all the Invoke methods are unrecognized.
andycruce 12-Nov-16 18:15pm    
I just tried something else. I took the thread code and put it in a Class contained in the main program. As soon as I put the code in a class

public class anything
{
thread code
}

the invoke methods are not recognized.

The problem is that your Thread1 class doesn't have the InvokeRequired property and BeginInvoke method. You're calling these against the Thread1 class instance (this). For these Invoke functions to be accessible, this must be the class that is the Form.
The simplest solution is to implement the FormWriteMethod in your Form-based class.
 
Share this answer
 
Remove the "using" line from here:
C#
namespace Thread_Test_Sending_Data_to_Thread_Function
{
    using System.Windows.Forms;
 
    public class Thread1
    {

You already have a using statement for that class, you don't need it again, and cannot use it inside a namespace or class.
 
Share this answer
 
v2
Comments
Matt T Heffron 14-Nov-16 14:51pm    
Actually, the using directive (this is not the using statement) is allowed inside a namespace (but not in a class). See the C# Language Spec, section 9.4.

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