2010-12-01 13 views
10

पर फ़ंक्शन को कॉल करने के लिए मुझे टाइमर पर फ़ंक्शन कॉल करने की आवश्यकता है (ट्यूटटाक() फ़ंक्शन पर कहें) और एएसपी.नेट एमवीसी प्रोजेक्ट में कुछ जानकारी पुनः लोड करें। मुझे पता है कि ऐसा करने के कई तरीके हैं, लेकिन आपकी राय से कौन सा सबसे अच्छा है?टाइमर एएसपी.नेट एमवीसी

नोट: फ़ंक्शन को केवल एक ही स्थान से बुलाया जाना चाहिए, और एप्लिकेशन को ऊपर तक हर एक्स मिनट कहा जाना चाहिए।

संपादित करें 1: कुछ जानकारी दोबारा लोड करें - उदाहरण के लिए मेरे पास कैश में कुछ है और मैं इसे टाइमर पर डीबी से अपडेट करना चाहता हूं - एक दिन में एक बार।

+2

हमें – rboarman

+0

पर क्या करने की कोशिश कर रहे हैं, इसके बारे में अधिक जानकारी चाहिए क्या आप कई तरीकों को सूचीबद्ध कर सकते हैं ताकि हम अपनी राय दे सकें? –

+0

क्यों एक अलग आवेदन नहीं है? –

उत्तर

24

इस सवाल आप से क्या मतलब है पर निर्भर करेगा बहुत ज्यादा का जवाब ASP.NET में कुछ जानकारी को फिर से लोड एमवीसी परियोजना। यह स्पष्ट रूप से निर्दिष्ट समस्या नहीं है और इस तरह, स्पष्ट रूप से अधिक से अधिक, इसका स्पष्ट रूप से उत्तर नहीं दिया जा सकता है।

तो हम यह मान करता है, तो यह है कि इस से आप समय-समय पर कुछ नियंत्रक कार्रवाई पोल और आप setInterval जावास्क्रिप्ट समारोह इस्तेमाल कर सकते हैं समय-समय पर एक AJAX अनुरोध भेजकर सर्वर पोल और यूआई अद्यतन करने के लिए एक दृश्य के बारे में जानकारी अद्यतन करना चाहते:

window.setInterval(function() { 
    // Send an AJAX request every 5s to poll for changes and update the UI 
    // example with jquery: 
    $.get('/foo', function(result) { 
     // TODO: use the results returned from your controller action 
     // to update the UI 
    }); 
}, 5000); 

दूसरी ओर आप नियमित रूप से समय में सर्वर पर कुछ कार्य को क्रियान्वित करने का अर्थ यह तो आप RegisterWaitForSingleObject विधि इस तरह इस्तेमाल कर सकते हैं:

var waitHandle = new AutoResetEvent(false); 
ThreadPool.RegisterWaitForSingleObject(
    waitHandle, 
    // Method to execute 
    (state, timeout) => 
    { 
     // TODO: implement the functionality you want to be executed 
     // on every 5 seconds here 
     // Important Remark: This method runs on a worker thread drawn 
     // from the thread pool which is also used to service requests 
     // so make sure that this method returns as fast as possible or 
     // you will be jeopardizing worker threads which could be catastrophic 
     // in a web application. Make sure you don't sleep here and if you were 
     // to perform some I/O intensive operation make sure you use asynchronous 
     // API and IO completion ports for increased scalability 
    }, 
    // optional state object to pass to the method 
    null, 
    // Execute the method after 5 seconds 
    TimeSpan.FromSeconds(5), 
    // Set this to false to execute it repeatedly every 5 seconds 
    false 
); 

आप कुछ और ही मतलब, hesi नहीं है अपने प्रश्न के लिए अधिक जानकारी प्रदान करने के लिए टेट।

+1

धन्यवाद दूसरा दूसरा मेरे मामले में पूरी तरह से काम करता है EDIT 1 – devfreak

+0

+1 स्पष्टीकरण की पूर्णता के लिए और प्रदर्शन चेतावनी – devfreak

+0

@ डारिन, मैं समय 4 बजे होने पर एक मेल भेजना चाहता हूं और जब डेटाबेस पंक्ति बदलती है, तो सिग्नल आर का उपयोग किए बिना कोई आसान तरीका है?, कोई भी मदद महान होगी। – stom

1

मैं एप्लिकेशन_स्टार्ट() से कार्यकर्ता धागा शुरू करके ऐसा करता हूं।

public interface IWorker 
{ 
    void DoWork(object anObject); 
} 

public enum WorkerState 
{ 
    Starting = 0, 
    Started, 
    Stopping, 
    Stopped, 
    Faulted 
} 

public class Worker : IWorker 
{ 
    public WorkerState State { get; set; } 

    public virtual void DoWork(object anObject) 
    { 
     while (!_shouldStop) 
     { 
      // Do some work 
      Thread.Sleep(5000); 
     } 

     // thread is stopping 
     // Do some final work 
    } 

    public void RequestStop() 
    { 
     State = WorkerState.Stopping; 
     _shouldStop = true; 
    } 
    // Volatile is used as hint to the compiler that this data 
    // member will be accessed by multiple threads. 
    protected volatile bool _shouldStop; 
} 

यह इस तरह शुरू कर दिया है:

यहाँ मेरी क्लास है

 var backgroundThread = new Thread(Worker.DoWork) 
            { 
             IsBackground = true, 
             Name = "MyThread" 
            }; 
     backgroundThread.Start(); 
11

आप Glbal.asax की OnApplicationStart घटना में टाइमर वर्ग का उपयोग कर सकते हैं ...

public static System.Timers.Timer timer = new System.Timers.Timer(60000); // This will raise the event every one minute. 
. 
. 
. 

timer.Enabled = true; 
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); 
. 
. 
. 

static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 
{ 
    // Do Your Stuff 
} 
0

मेरे समाधान इस तरह से है,

<script type="text/javascript"> 
    setInterval(
     function() 
     { 
      $.post("@Url.Action("Method", "Home")", {}, function (data) { 
       alert(data); 
      }); 
     }, 5000) 
</script> 

कॉल विधि;

[HttpPost] 
public ActionResult Method() 
{ 
    return Json("Tick"); 
} 
संबंधित मुद्दे