Refresh dataSource of DataGrid in Thread (WInForms)

Hi

In my application I need to refresh DataGrid every 10 sec. When I try to refresh data source in DG in thread method I got this message:
"A first chance exception of type 'System.ArgumentException' occurred in system.windows.forms.dll"

Does anybody could help me, or explain what should I do.

Thanks

Tom




Answer this question

Refresh dataSource of DataGrid in Thread (WInForms)

  • suparba

    Wow. Thanks

  • GreyW63

    The situation has changed and I want to create this in Web Application which contains only one aspx file in which there are 3 UserControl. And One of Them is the one with DataGrid.

    I have got sth like this(in USerControl). I am beginner as you can see. :). And I want to refresh this DAtaGrid every 10 seconds. Unfortunatelly it doesn't work. I am wondering where to stop this THread. Maybe in PageUnload method

    private void Page_Load(object sender, System.EventArgs e)

    {

    if(!Page.IsPostBack)

    {

    myThread= new Thread(new ThreadStart(ThreadMethod));

    myThread.Start();

    }

    }

    ......

    private void ThreadMethod()

    {

    while(true)

    {

    try

    {

    this.winnersDG.DataSource = DB.getWinners();

    this.winnersDG.DataBind();

    Thread.Sleep(10000);

    }

    catch(Exception e)

    {

    }

    }

    }



  • Yury_A

    Ok...lets think about what you are doing in an ASP.NET application for a second. You are going to create objects in memory, manipulate them, and then they are going to be rendered into html. That html will be sent to the user's browser over the internet. Now, if you put your thread to sleep for 10 seconds in a loop while the page is trying to load, what is that going to do It is going to prevent the form from ever finishing its load, and it will never render its results to the browser.

    The point is that ASP.NET forms do not have a permanent lifecycle like Windows forms do. They are created, rendered, and destroyed. Viewstate is used to maintain state between incarnations of the form, but the form object, and the thread that is processing it, does not live past the point the point at which it is rendered into html (roughly).

    The only way you can accomplish a refresh is by putting in client-side code to ask the browser to refresh the page. You can do this using a meta tag (http://webdesign.about.com/cs/metatags/a/aa080300a.htm) or javascript (http://grizzlyweb.com/webmaster/javascripts/refresh.asp).

    You might also want to get a better understanding of the lifecycle of forms in ASP.NET: http://www.15seconds.com/issue/020102.htm



  • RichardGreenwell

    You will need to post more information. What is the message of the exception (the argument and why it is invalid) What is the stack trace What is the line of code it occurs on How are you "refreshing" the datagrid

  • tbrierly

    Hi,

    what about same thing in VB.Net

    Thanks


  • Refresh dataSource of DataGrid in Thread (WInForms)