1

When I try an connect to an instance that does not exist the UI locks up in my application I have tried using a new thread but this does not seem to work

//call from main class

 Dispatcher.BeginInvoke(new Action(
 () => Timedtask(instance, database)));    

 //Method

  public void Timedtask(string instance, string database)
        { 
            Timer.Start();
            Timer.Tick += delegate
            {
                if (!TimedTask.timer_Tick(instance, database))
                {
                    Dispatcher.CurrentDispatcher.Invoke(Stopped);
                }
            };

        }   

    // Try an open connection

         using (var con1 = new SqlConnection
                {
                    ConnectionString = @"Data Source=" + instanceName1 + ";Integrated Security=SSPI;" +
                 "MultipleActiveResultSets=true;"
                })


                {

                //more code
                 con1.Open();               
                //more code
                }
sw2020
  • 195
  • 1
  • 2
  • 10

5 Answers5

8

Dispatcher.BeginInvoke puts the call back on UI thread and not on background thread. So, all your operations are running on UI thread. Hence locking up your UI.

Use Task or BackgroundWorker to delegate call on background thread. Just be aware that any GUI component can't be modified from background thread, so you need to marshall that back on UI thread using Dispatcher.Invoke or Dispatcher.BeginInvoke.


Task.Factory.StartNew(() => { Timedtask(instance, database) });

OR

If using .Net 3.5, you can use backgroundWorker:

BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (s, e) => { Timedtask(instance, database); };
worker.RunWorkerAsync();
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
4

I like using BackgroundWorkers

Setup:

private BackgroundWorker bw;
bw.DoWork += bw_DoWork;
bw.RunWorkerCompleted += bw_WorkCompleted;

Implement these methods:

protected void bw_DoWork(object sender, DoWorkEventArgs e)
{
    //Do database stuff here
}

protected void bw_WorkCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    //This is called when the background worker is done.  This is where you can update the UI
}

Then to start the worker:

bw.RunWorkerAsync();
Tyler
  • 17,669
  • 10
  • 51
  • 89
2

you can use tasks

Task.Factory.StartNew(() =>
            {
                using (var con1 = new SqlConnection
                {
                    ConnectionString = @"Data Source=" + instanceName1 + ";Integrated Security=SSPI;" +
                 "MultipleActiveResultSets=true;"
                })
                {

                    //more code
                    con1.Open();
                    //more code
                }
            });
Akrem
  • 5,033
  • 8
  • 37
  • 64
0

You can solve this kind of problem by using BackgroundWorker. Here, you can find detailed and useful information about the usage and implementation of BackgroundWorker in a WPF application: How to use WPF Background Worker

Community
  • 1
  • 1
rPulvi
  • 946
  • 8
  • 33
0

TPL is better than BackgroundWorker.Try to use TPL(Task Parrel Liaberary) because the Task Parellel class is an improvement over the BackgroundWorker. It naturally supports nesting (parent/child tasks), uses the new cancellation API, task continuations, etc.

Please open below link of Stephen Cleary about "Reporting Progress from Tasks":

Reporting Progress from Tasks

Gul Ershad
  • 1,743
  • 2
  • 25
  • 32