C# 执行搜索wpf时超时

C# 执行搜索wpf时超时,c#,.net,wpf,C#,.net,Wpf,当我在我的应用程序中执行搜索时,我希望有一个超时函数,它会给用户一条消息,并在10秒后停止搜索 这怎么可能?你能给我看一下密码吗 如果您需要使用我的代码,请告诉我您需要查看的部分 多谢各位 你好!!这是代码。忘了告诉你我在使用后台工作人员做你发布的建议不起作用 我想为这件作品设定一个期限 私有void worker_DoWork(对象发送方,DoWorkEventArgs e) { 进度=0; 秒表sw=新秒表(); sw.Start(); var donewithwork=false

当我在我的应用程序中执行搜索时,我希望有一个超时函数,它会给用户一条消息,并在10秒后停止搜索

这怎么可能?你能给我看一下密码吗

如果您需要使用我的代码,请告诉我您需要查看的部分

多谢各位

你好!!这是代码。忘了告诉你我在使用后台工作人员做你发布的建议不起作用

我想为这件作品设定一个期限

私有void 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;
        }
    }
while(sw.elapsedmillishes<5000&&!donewithwork)
{
if(m_oWorker.CancellationPending==true)
{
e、 取消=真;
}
else if(donewithwork!=真)
{
{
尝试
{
进展=10;
MongoServer设置=新建MongoServer设置();
settings.Server=新的MongoServer地址(“lysing.uia.no”,27017);
MongoServer服务器=新的MongoServer(设置);
MongoDatabase=server.GetDatabase(“tweet_数据库”);
var collection=database.GetCollection(“文档”);
var query=query.And(query.Matches(“text”,新的BsonRegularExpression(新的Regex(searchText,RegexOptions.IgnoreCase)))
/*,Query.Near(“geo”,58.1453,7.95711000,true)*/*,Query.Matches(“创建于”,“一月”)*/);
var cursor=collection.Find(查询);
进展=20;
//将光标限制设置为单选按钮指示的四个相同值(50100500和1000)
如果(游标限制!=0)
{
cursor.SetLimit(cursorLimit);
}
//如果未选取任何值(等于0),则会出现此消息框
其他的
{
光标。设置限制(10);
MessageBox.Show(“verdi påhøyre侧的måvelge.标准verdi er 10”);
}
进展=30;
控制台。写入(“2”);
/*线程t=新线程(TimeTick);
t、 Start()*/
控制台写入(“运行时间1为:”+sw.ElapsedMilliseconds);
if(cursor.Size()==0)
{
控制台。写入(“3”);
MessageBox.Show(“Ditt søk returnerte ingen treff.Vennligst prøv et annet søkeord”);
控制台写入(“经过的过期时间为:“+sw.经过”);
//t.Join();
打破
}
进展=40;
控制台。写入(“4”);
控制台写入(“已用时间2为:“+sw.已用”);
if(m_oWorker.CancellationPending==true)
{
e、 取消=真;
}
App.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,新线程开始(()=>
MainViewModel.TweetOC.Clear());
App.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,新线程开始(()=>
MainViewModel.GeoOC.Clear());
进展=50;
foreach(游标中的变量项)
if(m_oWorker.CancellationPending==true)
{
e、 取消=真;
}
else if(item.geo!=null&&item.text!=null)
{
App.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,新线程开始(()=>
GeoOC.Add(项目));
添加(项目);
}
其他的
{
App.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,新线程开始(()=>
MainViewModel.TweetOC.Add(item));
}
进展=60;
var size=cursor.size();
如果(游标限制==0)
{
粗略限制=10;
}
if(m_oWorker.CancellationPending==true)
{
e、 取消=真;
}
如果(
MessageBox.Show(“Du fikk”+“”+大小+“”+treff av”+“”+光标.Count()+“”+mulige。\r\n”+“”+
“Ensker duåhente ut de neste”+“”+cursorLimit+“”+Svarene også?,“Antall mulig treff”,MessageBoxButtons.YesNo)=DialogResult.Yes)
{   
var query2=Query.And(Query.Matches(“text”,新的BsonRegularExpression(新的Regex(searchText,RegexOptions.IgnoreCase)));
var cursor2=collection.Find(query2);
游标2.Skip=游标限制;
游标2.限制=游标限制;
foreach(游标2中的变量项)
if(m_oWorker.CancellationPending==true)
    // The AutoResetEvent is here to count time
    private AutoResetEvent event = new AutoResetEvent(false);

private YourMethod()
{
    Thread thread = new Thread(new ThreadStart(YourSearch));
    thread.Start();

    if (event.WaitOne(10000) == false)
    {
    // This should be reached only if the thread hasn't finished executing the search method after 10 seconds (10000 ms, you can change the parameter)
    // Show a message or whatever you want here, then abort thread:
    thread.Abort();
    }
}
private void YourSearchMethod()
{
// Search whatever you want;
event.Set(); // Sets event (which triggers the WaitOne() call we used earlier)
}