Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 23rd, 2004, 11:43 AM
Registered User
 
Join Date: Jun 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Windows Forms Threading

I have a windows that uses a separate thread to run a sequence of commands (like ping) so that the GUI remains responsive. This works well.

However, I also have a datagrid that I want to update after each "ping" but whenever I do this I get the following error :-

Controls created on one thread cannot be parented to a control on a different thread

This is because you cannot update something on the form from a thread that didnt start it and can be got around using "delegates". Unfoturnately delegates are currently beyond my learning at the moment.

Can anyone explain in simple terms what I need to to get around this ?

Some code :-

public void btnGo_Click(object sender, System.EventArgs e)
{
// Start a new thread to run the batch processes in
Thread oThread = new Thread(new ThreadStart(startProcessingThread));
oThread.Start();
}

private void startProcessingThread()
{
    // Get all the filenames to be processed
    FileInfo[] fiFileNames = getFileNames(tbInputDir.Text);
    int i = 1;
    foreach (FileInfo file in fiFileNames)
    {
        startProcessing(file.Name);
    i++;
     MakeNewDataSet();
    }
}

This works well up to the point I need run MakeNewDataSet() to update the datagrid and I get the above error.

Any help appreciated.
 
Old January 23rd, 2004, 05:05 PM
Authorized User
 
Join Date: Jun 2003
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Look up MethodInvoker. It's a kind of delegate you can create to call a function or methods from a thread.

It's important for us to explain to our nation that life is important. It's not only life of babies, but it's life of children living in, you know, the dark dungeons of the Internet."— George W. Bush - Arlington Heights, Ill., Oct. 24, 2000
 
Old January 27th, 2004, 07:50 AM
Registered User
 
Join Date: Jun 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thankyou for the reply. I used MethodInvoker as you suggested and it now updates well.

I think maybe some more reading up on delegates may help as well, just in case I need them in the future. :-)

J
 
Old October 5th, 2006, 05:21 AM
Authorized User
 
Join Date: Jun 2006
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Default

HI

How to write MethodInvoker ?Can u give me some solution?

Thankx
monika






Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows Forms sambathrajmca Windows Workflow 1 November 27th, 2007 06:25 AM
windows forms lakshmiR .NET Framework 1.x 1 August 31st, 2007 08:32 PM
windows forms chandrasekhar ASP.NET 2.0 Professional 2 February 27th, 2006 10:41 PM
Windows Form Threading Problem nugie73 VB.NET 0 August 25th, 2004 06:12 AM
Windows forms eyan C# 1 July 1st, 2004 04:10 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.