Click here to Skip to main content
15,913,570 members
Home / Discussions / C#
   

C#

 
GeneralRe: Save Text as Image Pin
CKnig19-Feb-08 18:12
CKnig19-Feb-08 18:12 
GeneralRe: Save Text as Image Pin
D i x y19-Feb-08 18:44
D i x y19-Feb-08 18:44 
Generalreferencing dlls with same name. Pin
D2raghu19-Feb-08 17:08
D2raghu19-Feb-08 17:08 
GeneralRe: referencing dlls with same name. Pin
Hamza Nadim20-Feb-08 5:00
Hamza Nadim20-Feb-08 5:00 
GeneralGAC related question Pin
Imtiaz Murtaza19-Feb-08 13:24
Imtiaz Murtaza19-Feb-08 13:24 
GeneralRe: GAC related question Pin
Luc Pattyn19-Feb-08 13:56
sitebuilderLuc Pattyn19-Feb-08 13:56 
GeneralRe: GAC related question Pin
PIEBALDconsult19-Feb-08 14:04
mvePIEBALDconsult19-Feb-08 14:04 
QuestionThread instance object location Pin
Spacix One19-Feb-08 13:01
Spacix One19-Feb-08 13:01 
Is it wrong to use an instance property to hold the instance of a thread?

Example code of what I mean:
using System;
using System.Threading;
using System.Collections;
using System.Windows.Forms;
using System.IO;
using newThreads;

namespace newThreads
{
    class newThread
    {
        private uint i;
        newThread()
        {
            i=0;            
        }
        newThread(uint I)
        {
            i=I;            
        }
        newThread(int I)
        {
            i=Convert.ToUInt32(I);            
        }
        private volatile bool bExit = false;
        public void threadMethod()
        {
            /* do stuff before form loads ect... */
            while (!bExit)
            {
                ++i;
                Thread.Sleep(1500);
            }
        }
        public void ResetThread()
        {
            i = 0;
        public void ThreadExit()
        {
            bExit = false;
        }
    }
}
namespace newApp
{
    public class winFormApp : System.Windows.Forms.Form
    {
        private newThreads _newThread;
        public newThreads newThread
        {
            get
            {
                return _newThread;
            }
            set
            {
                _newThread = value;
            }
        }

        public winFormApp()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            //
            // User constructor code after InitializeComponent call
            //
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code
        private void InitializeComponent()
        {
            /* clip for size */
        }
        #endregion

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();

            using (winFormApp mainForm = new winFormApp())
            {
                mainForm.newThread = new newThreads.newThread();

                Thread newThreadID = new Thread(mainForm.newThread.threadMethod);
                newThreadID.Start();

                while (!newThreadID.IsAlive)
                {
                    Thread.Sleep(1);
                }

                Application.Run(mainForm);

                mainForm.newThread.ThreadExit();
                newThreadID.Join(new TimeSpan(0, 0, 3));
                if (newThreadID.IsAlive)
                {
                    newThreadID.Abort();
                    File.AppendAllText("newApp.log", "Forcefully killed new thread, join() timed out." + Environment.NewLine, System.Text.Encoding.ASCII);
                }
            }
            return;
        }
    }
}


Only reason for this is I wish to call other methods (besides the exit one) in the thread via the winForm instance, and this is the first way I thought of on how to get a hold of the thread's instance inside the winFrom. Well, without starting the thread inside the winForm, but that was ruled out because the thread needs to do stuff before the form loads.

The problem is I don't like the looks of it, but at the same time it does what I'm needing to do. I can't think of a reason not to do it, so I'm asking for other solution suggestions / comments on mine.

Also would like to know if this could be considered bad practice...


-Spacix
All your skynet questions[^] belong to solved

GeneralRe: Thread instance object location Pin
Luc Pattyn19-Feb-08 14:25
sitebuilderLuc Pattyn19-Feb-08 14:25 
GeneralRe: Thread instance object location Pin
Spacix One19-Feb-08 17:08
Spacix One19-Feb-08 17:08 
GeneralRe: Thread instance object location Pin
Luc Pattyn20-Feb-08 4:07
sitebuilderLuc Pattyn20-Feb-08 4:07 
GeneralReflectively setting properties on value types Pin
Skippums19-Feb-08 12:06
Skippums19-Feb-08 12:06 
AnswerRe: Reflectively setting properties on value types Pin
Skippums19-Feb-08 12:25
Skippums19-Feb-08 12:25 
GeneralRe: Reflectively setting properties on value types Pin
PIEBALDconsult19-Feb-08 13:13
mvePIEBALDconsult19-Feb-08 13:13 
GeneralRe: Reflectively setting properties on value types Pin
PIEBALDconsult19-Feb-08 13:06
mvePIEBALDconsult19-Feb-08 13:06 
GeneralRe: Reflectively setting properties on value types Pin
Skippums20-Feb-08 6:46
Skippums20-Feb-08 6:46 
Questionp-port output problem Pin
Gunnar575919-Feb-08 11:25
Gunnar575919-Feb-08 11:25 
GeneralRe: p-port output problem Pin
Christian Graus19-Feb-08 12:32
protectorChristian Graus19-Feb-08 12:32 
QuestionData table in C# Pin
dalbhide bipin19-Feb-08 11:12
dalbhide bipin19-Feb-08 11:12 
GeneralRe: Data table in C# Pin
Gareth H19-Feb-08 11:35
Gareth H19-Feb-08 11:35 
GeneralRe: Data table in C# Pin
dalbhide bipin19-Feb-08 18:47
dalbhide bipin19-Feb-08 18:47 
GeneralArray null problem Pin
netJP12L19-Feb-08 10:58
netJP12L19-Feb-08 10:58 
GeneralRe: Array null problem Pin
Ravi Bhavnani19-Feb-08 11:03
professionalRavi Bhavnani19-Feb-08 11:03 
GeneralRe: Array null problem Pin
TheGreatAndPowerfulOz20-Feb-08 13:32
TheGreatAndPowerfulOz20-Feb-08 13:32 
Questionhow to create web site blocker Pin
Mahmood Abbasi19-Feb-08 10:06
Mahmood Abbasi19-Feb-08 10:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.