Threading and Forms 
Author Message
 Threading and Forms

I'm new to C# and will be doing a final year project for university next
year using the language.  I come from a VB background.

I've got a simple threading test application (testing before making a server
app) which allows the user to specify number of threads.  Some of the code
is used from examples given in this newsgroup (thanks people!).

Originally, before I changed the code from calling public void DoWork() to
calling a new instance of the class MyThread(),  I was able to output lines
of 'log' information to a listbox on Form1 (lbLog), as the call was made
within the same class.

How can I access this same listbox from the class handling the threads?

The code (stripped down a bit) is as follows for the main button click
event, the old DoWork() and the new MyThread() class.

private void button1_Click(object sender, System.EventArgs e)
{
    int noThreads;
    noThreads = int.Parse(txtNumber.Text);

   for(int i=0;i<noThreads;i++)
    {
        MyThread tester = new MyThread();
        tester.id = i;
        tester.threadoutput = "Thread active";
        Thread tGo = new Thread(new ThreadStart(tester.Main));
        tGo.Start();
    }

Quote:
}

public void DoWork()
{
    string id = Thread.CurrentThread.GetHashCode().ToString();
    int repetitions = int.Parse(txtRep.Text);
    lbLog.Items.Add("Thread " + id + " has started");

    for(int i=1;i<=repetitions;i++)
    {
    string datetime = DateTime.Now.ToString();
    lbLog.Items.Add("Logging Thread" + id + " : " + i.ToString() + " : " +
datetime);
    Thread.Sleep(5);
    }

Quote:
}

public class MyThread
{
    public object id;
    public object threadoutput;

    public void Main()
    {
    MessageBox.Show("Received id: " + id + ", Output:" +
threadoutput,"Thread " + id + " Result");
    //I want to write to the Log HERE
    }

Quote:
}

Thanks in advance for any guidance or info.

Jon Cooper



Thu, 30 Dec 2004 18:34:38 GMT  
 Threading and Forms
Use some form of code protection. Mutex will be nice
choice.


Thu, 30 Dec 2004 19:13:44 GMT  
 Threading and Forms
I don't think it's advisable to write to UI controls from
threads other than the main UI thread.

What I would suggest you do is to create a synchronized
stack or queue object that all the threads dump into.

When the threads are finished, the controller class will
then empty this stack or queue into the log listview
control.

-c


Quote:
> I'm new to C# and will be doing a final year project for university next
> year using the language.  I come from a VB background.

> I've got a simple threading test application (testing before making a
server
> app) which allows the user to specify number of threads.  Some of the code
> is used from examples given in this newsgroup (thanks people!).

> Originally, before I changed the code from calling public void DoWork() to
> calling a new instance of the class MyThread(),  I was able to output
lines
> of 'log' information to a listbox on Form1 (lbLog), as the call was made
> within the same class.

> How can I access this same listbox from the class handling the threads?

> The code (stripped down a bit) is as follows for the main button click
> event, the old DoWork() and the new MyThread() class.

> private void button1_Click(object sender, System.EventArgs e)
> {
>     int noThreads;
>     noThreads = int.Parse(txtNumber.Text);

>    for(int i=0;i<noThreads;i++)
>     {
>         MyThread tester = new MyThread();
>         tester.id = i;
>         tester.threadoutput = "Thread active";
>         Thread tGo = new Thread(new ThreadStart(tester.Main));
>         tGo.Start();
>     }
> }

> public void DoWork()
> {
>     string id = Thread.CurrentThread.GetHashCode().ToString();
>     int repetitions = int.Parse(txtRep.Text);
>     lbLog.Items.Add("Thread " + id + " has started");

>     for(int i=1;i<=repetitions;i++)
>     {
>     string datetime = DateTime.Now.ToString();
>     lbLog.Items.Add("Logging Thread" + id + " : " + i.ToString() + " : " +
> datetime);
>     Thread.Sleep(5);
>     }
> }

> public class MyThread
> {
>     public object id;
>     public object threadoutput;

>     public void Main()
>     {
>     MessageBox.Show("Received id: " + id + ", Output:" +
> threadoutput,"Thread " + id + " Result");
>     //I want to write to the Log HERE
>     }
> }

> Thanks in advance for any guidance or info.

> Jon Cooper



Thu, 30 Dec 2004 23:41:52 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Threading and Forms

2. Awakening the UI thread from another thread in a Windows Form application

3. Windows forms and background threads

4. Forms in Threads Question

5. Threading error in form called from legacy C++ app (Was: Debugging with drag and drop)

6. Creating Dynamic Forms in a thread.

7. Form Instances via Threading.

8. creating form in thread

9. Closing the form by its thread id.

10. creating form in thread

11. Please Help - Adding Controls to a form from a child thread

12. Accessing Form Objects from within a Thread

 

 
Powered by phpBB® Forum Software