1

I want to receive notification from database when table is updated but I need a background thread which call after 1 mint and check database table and show me notification. How can I implement a background thread? I have implemented a thread but when I use while(true) in it, my form is not loaded keep on processing.

Code:

protected void Page_Load(object sender, EventArgs e)
{
    t1 = new Thread(new ThreadStart(Function1));
    t1.IsBackground = true;
    t1.Start();
}

private void Function1()
{
    while (true)
    {
        Thread.Sleep(2000);
        count++;                
        Label1.Text = "Function" + count;
    }
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Jhon Peter
  • 13
  • 1
  • 1
  • 9

3 Answers3

1

You can not use background thread on asp.net page. It works on http stateless protocol and the page object is not available after the response it sent. You can only send one response against one request. You can use jQuery ajax , asp.net ajax library timer control or web sockets to fetch data from server periodically.

This post explains how you can fetch data from server using jQuery ajax. This is also very good tutorial to get the data from server using web methods. The example from this tutorial is given below.

Code behind

public partial class _Default : Page 
{
  [WebMethod]
  public static string GetDate()
  {
    return DateTime.Now.ToString();
  }
}

Javascript

$(document).ready(function() {
  // Add the page method call as an onclick handler for the div.
  $("#Result").click(function() {
    $.ajax({
      type: "POST",
      url: "Default.aspx/GetDate",
      data: "{}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(msg) {
        // Replace the div's content with the page method's return.
        $("#Result").text(msg.d);
      }
    });
  });
});
Community
  • 1
  • 1
Adil
  • 146,340
  • 25
  • 209
  • 204
  • I have link to the post that can help you, http://stackoverflow.com/questions/5052543/how-to-fire-ajax-request-periodically – Adil Feb 21 '14 at 10:38
  • This code refresh my whole page i just wana refresh my label through timer_tick is it possible.....? – Jhon Peter Feb 21 '14 at 10:43
1

You have a fundamental misunderstanding of the difference between server-side and client-side code.

For example, your request would require a framework such as SignalR to push a real-time notification to a client.

The easier method is to use Javascript to poll a page of your choice.. for example (jQuery, obviously):

// I'm not a huge jQuery person so theres probably a jQuery way to do this
setInterval(function() { 
    $.get('/yourPage.aspx', function(response) {
        if (response.success) {
            alert(response.message);
        }
    });
}, 5000); // poll a page every 5 seconds.

Then your C# page at /yourPage.aspx can check the database and return a Json object with the properties I've mentioned above.

You need to read up on the difference between Client Side and Server Side.. and how they interact in a stateless protocol such as HTTP.

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
0

You can do async tasks in ASP.NET if you are using the 4.5 framework:

http://www.hanselman.com/blog/TheMagicOfUsingAsynchronousMethodsInASPNET45PlusAnImportantGotcha.aspx

Brian Ogden
  • 18,439
  • 10
  • 97
  • 176