Reputation: 81
I am trying to build an application on MONO using Backgroundworker. My application is working fine when i use the sequential method. Basically my application draws some rectangles the on the drawing area on click of the button.
I am trying to compare the difference in execution time between sequential, using Backgroundworker and others.
I have a problem in using background worker, have a look at the following code,
static void DoWork (object sender, DoWorkEventArgs e)
{
Context ct = (Context)e.Argument;
house.DrawHouse rect = new house.DrawHouse ();
PointD p1, p2, p3, p4;
p1 = new PointD (55, 250);
p2 = new PointD (65, 250);
p3 = new PointD (65, 90);
p4 = new PointD (55, 90);
Gtk.Application.Invoke (delegate {
ct.MoveTo (p1);
ct.LineTo (p2);
ct.LineTo (p3);
ct.LineTo (p4);
ct.LineTo (p1);
ct.ClosePath ();
ct.Color = new Color (0, 0, 0);
ct.FillPreserve ();
ct.Color = new Color (255, 255, 255);
ct.Stroke ();
});
}
}
in the above code worker thread is creating creating the rectangle and giving to the GUI thread to print it. but it is throwing an error in the ct.MoveTo (p1);
Hope to hear from some one soon.
Upvotes: 2
Views: 1037
Reputation: 81
You are correct. Yes, I am running this on windows. and for the first point of yours, worker thread will start the task and create the rectangle and gives to GUI thread to print it on the screen. (Gtk.Application.Invoke (delegate { }); this will switch to GUI thread)
Have a look at the following code I am doing the same thing but not in perspective of painting but it does some time consuming calculation. The calculation which is time consuming is done by the worker thread and giving the result to GUI thread to print on the UI. This is absolutely working fine for me with out any error.
void DoWork (object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
var watch = Stopwatch.StartNew ();
for (int i = lowerLimit; i <= upperLimit; i++) {
int j = i;
var result = SumRootN (j);
Gtk.Application.Invoke (delegate {
textview15.Buffer.Text += "root " + j.ToString () + " = " + result.ToString () + Environment.NewLine;
});
worker.ReportProgress ((int)(((double)i / (double)upperLimit) * 100));
}
var time = watch.ElapsedMilliseconds;
textview20.Buffer.Text = "Background Worker: \n execution took " + time + " MS to \n complete the execution \n SLOW & RESPONSIVE";
}
But if i do the same thing on for painting it is giving me the error i attached above.
Please correct me if i am wrong
thanks for the reply knocte, I will have look at your response.
Upvotes: 0
Reputation: 17939
Are you sure you're calling that code in the GUI thread? To find out, you could use this tool.
Also, let me guess: you're running this on Windows, aren't you? I got this impression because AccessViolationExceptions happen more often on MS.NET than on Mono (somehow, the interoperability with the unmanaged world is less strict on Mono).
This has been brought up sometimes in bugs, but not yet fixed. In MS.NET these issues are normally fixed by applying proper calling conventions. An example is this, maybe you're missing some of this? (Or Cairo's binding? be sure to clone gtk-sharp and have a look at the DllImport being called; maybe you can spot a bug there and send a pull request to fix it?)
Upvotes: 1