當我在我的應用程序中執行搜索時,我想要有一個超時函數,它給用戶一條消息並在10秒後停止搜索。執行搜索時超時wpf
這怎麼可能?你能告訴我那個代碼嗎?
讓我知道如果你需要我的代碼和你需要看到的部分。
謝謝
您好!這是守則。忘了告訴你即時通訊使用背景工作者做你張貼的建議不會工作。
欲設定時限這塊
私人無效worker_DoWork(對象發件人,DoWorkEventArgs E) { 進展= 0; 秒錶sw =新秒錶(); sw.Start(); var donewithwork = false;
while (sw.ElapsedMilliseconds < 5000 && !donewithwork)
{
if (m_oWorker.CancellationPending == true)
{
e.Cancel = true;
}
else if (donewithwork != true)
{
{
try
{
progress = 10;
MongoServerSettings settings = new MongoServerSettings();
settings.Server = new MongoServerAddress("lysing.uia.no", 27017);
MongoServer server = new MongoServer(settings);
MongoDatabase database = server.GetDatabase("tweet_database");
var collection = database.GetCollection<Tweets>("docs");
var query = Query.And(Query.Matches("text", new BsonRegularExpression(new Regex(searchText, RegexOptions.IgnoreCase)))
/*, Query.Near("geo",58.1453, 7.9571, 1000, true)*//*, Query.Matches("created_at", "Jan")*/);
var cursor = collection.Find(query);
progress = 20;
// Sets the cursorLimit to the four same values as the radiobuttons indicates (50,100,500 and 1000)
if (cursorLimit != 0)
{
cursor.SetLimit(cursorLimit);
}
// If no value is picked (equals 0), this messagebox will appear
else
{
cursor.SetLimit(10);
MessageBox.Show("Du må velge en verdi på høyre side. Standard verdi er 10. ");
}
progress = 30;
Console.Write("2");
/* Thread t = new Thread(TimeTick);
t.Start();*/
Console.Write("Elapsed time1 is:" + sw.ElapsedMilliseconds);
if (cursor.Size() == 0)
{
Console.Write("3");
MessageBox.Show("Ditt søk returnerte ingen treff. Vennligst prøv et annet søkeord.");
Console.Write("Elapsed expired time is:" + sw.Elapsed);
// t.Join();
break;
}
progress = 40;
Console.Write("4");
Console.Write("Elapsed time2 is:" + sw.Elapsed);
if (m_oWorker.CancellationPending == true)
{
e.Cancel = true;
}
App.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new ThreadStart(() =>
MainViewModel.TweetOC.Clear()));
App.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new ThreadStart(() =>
MainViewModel.GeoOC.Clear()));
progress = 50;
foreach (var item in cursor)
if (m_oWorker.CancellationPending == true)
{
e.Cancel = true;
}
else if (item.geo != null && item.text != null)
{
App.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new ThreadStart(() =>
GeoOC.Add(item)));
TweetOC.Add(item);
}
else
{
App.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new ThreadStart(() =>
MainViewModel.TweetOC.Add(item)));
}
progress = 60;
var size = cursor.Size();
if (cursorLimit == 0)
{
cursorLimit = 10;
}
if (m_oWorker.CancellationPending == true)
{
e.Cancel = true;
}
if (
MessageBox.Show("Du fikk" + " " + size + " " + "treff av" + " " + cursor.Count() + " " + "mulige. \r\n" + " " +
"Ønsker du å hente ut de neste" + " " + cursorLimit + " " + "Svarene også?", "Antall mulig treff", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
var query2 = Query.And(Query.Matches("text", new BsonRegularExpression(new Regex(searchText, RegexOptions.IgnoreCase))));
var cursor2 = collection.Find(query2);
cursor2.Skip = cursorLimit;
cursor2.Limit = cursorLimit;
foreach (var item in cursor2)
if (m_oWorker.CancellationPending == true)
{
e.Cancel = true;
}
else if (item.geo != null && item.text != null)
{
App.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new ThreadStart(() =>
GeoOC.Add(item)));
TweetOC.Add(item);
}
else
{
App.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new ThreadStart(() =>
MainViewModel.TweetOC.Add(item)));
}
}
progress = 70;
System.Console.WriteLine("TweetViewCount:" + TweetOC.Count);
System.Console.WriteLine("GeoViewCount:" + GeoOC.Count);
Console.Write("Elapsed time3 is:" + sw.Elapsed);
// t.Join();
progress = 100;
//return database;
}
catch (Exception)
{
throw new Exception("Something went wrong");
}
}
donewithwork = true;
}
else MessageBox.Show("Your search did not return a match. Please search for something else.");
progress = 0;
}
}
歡迎堆棧溢出!雖然這可能在理論上回答這個問題,[這將是更可取的](http://meta.stackexchange.com/q/8259)在這裏包括答案的重要部分,並提供供參考的鏈接。 – dcastro