cross thread operations
-
look at this situation(this is just an example for clarification): i have 2 threads: thread1: is my main thread,( the static Program class) and should for example Run a form. thread2: is my sec thread and should show a Splash screen that should be displayed according to my main thread. thread1(main) wants to update a progressbar on thread2. i am a little confused... :wtf: my solution: i guess that i could solve this by implementing events, rising them on thread1 and Handling them on thread2 and do the requested job accordingly(i.e. updating a progress). but sth new arises and that was Cross-Thread-Oper... , and there is no Invoke Metod for my this obj (that is Static Program class, don't call me stupid) to prevent this exception(cross-thread-...excption).
-
look at this situation(this is just an example for clarification): i have 2 threads: thread1: is my main thread,( the static Program class) and should for example Run a form. thread2: is my sec thread and should show a Splash screen that should be displayed according to my main thread. thread1(main) wants to update a progressbar on thread2. i am a little confused... :wtf: my solution: i guess that i could solve this by implementing events, rising them on thread1 and Handling them on thread2 and do the requested job accordingly(i.e. updating a progress). but sth new arises and that was Cross-Thread-Oper... , and there is no Invoke Metod for my this obj (that is Static Program class, don't call me stupid) to prevent this exception(cross-thread-...excption).
Thread1 should call Invoke/BeginInvoke on the progressBar in Thread2.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
look at this situation(this is just an example for clarification): i have 2 threads: thread1: is my main thread,( the static Program class) and should for example Run a form. thread2: is my sec thread and should show a Splash screen that should be displayed according to my main thread. thread1(main) wants to update a progressbar on thread2. i am a little confused... :wtf: my solution: i guess that i could solve this by implementing events, rising them on thread1 and Handling them on thread2 and do the requested job accordingly(i.e. updating a progress). but sth new arises and that was Cross-Thread-Oper... , and there is no Invoke Metod for my this obj (that is Static Program class, don't call me stupid) to prevent this exception(cross-thread-...excption).
-
Thread1 should call Invoke/BeginInvoke on the progressBar in Thread2.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus)I know the Invoke procedure, but we can use Invoke when we have a Form!(Form.Invoke()) my threads are implemented in Program class where i Run my application(and there is no this.Invoke method!): look here: my Form1 obj(f) during its lifetime will raise some events! i want to handle those events from thread2 and do the jobs in thread2!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Server
{
static class Program
{
static System.Threading.Thread thread2 = null;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Form1 f = new Form1();
thread2 = new System.Threading.Thread(new System.Threading.ThreadStart(worker));
thread2.IsBackground = true;
thread2.ApartmentState = System.Threading.ApartmentState.STA;
thread2.Start();
try
{
f.job1 += new EventHandler(Job1);
f.job2 += new EventHandler(Job2;
f.job3 += new EventHandler(Job3);
Application.Run(f);
}
catch(Exception r)
{
}
}
static void worker()
{
//create a splash screen and update its progress due to events job1,job2 and job3
}
} -
I know the Invoke procedure, but we can use Invoke when we have a Form!(Form.Invoke()) my threads are implemented in Program class where i Run my application(and there is no this.Invoke method!): look here: my Form1 obj(f) during its lifetime will raise some events! i want to handle those events from thread2 and do the jobs in thread2!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Server
{
static class Program
{
static System.Threading.Thread thread2 = null;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Form1 f = new Form1();
thread2 = new System.Threading.Thread(new System.Threading.ThreadStart(worker));
thread2.IsBackground = true;
thread2.ApartmentState = System.Threading.ApartmentState.STA;
thread2.Start();
try
{
f.job1 += new EventHandler(Job1);
f.job2 += new EventHandler(Job2;
f.job3 += new EventHandler(Job3);
Application.Run(f);
}
catch(Exception r)
{
}
}
static void worker()
{
//create a splash screen and update its progress due to events job1,job2 and job3
}
}Someone has asked a similar question to yours, here[^], perhaps you can get some ideas from there. There are also several articles here on CodeProject. Use the Search articles box, at the top of the page, and search for threaded splash screen, or similar.
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
Thread1 should call Invoke/BeginInvoke on the progressBar in Thread2.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
In general speaking, i want to handle events that raise by thread1 and process them with thread2, that's all!
-
I know the Invoke procedure, but we can use Invoke when we have a Form!(Form.Invoke()) my threads are implemented in Program class where i Run my application(and there is no this.Invoke method!): look here: my Form1 obj(f) during its lifetime will raise some events! i want to handle those events from thread2 and do the jobs in thread2!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Server
{
static class Program
{
static System.Threading.Thread thread2 = null;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Form1 f = new Form1();
thread2 = new System.Threading.Thread(new System.Threading.ThreadStart(worker));
thread2.IsBackground = true;
thread2.ApartmentState = System.Threading.ApartmentState.STA;
thread2.Start();
try
{
f.job1 += new EventHandler(Job1);
f.job2 += new EventHandler(Job2;
f.job3 += new EventHandler(Job3);
Application.Run(f);
}
catch(Exception r)
{
}
}
static void worker()
{
//create a splash screen and update its progress due to events job1,job2 and job3
}
}Does the worker() method create a Form? What do the event handlers for Job1, Job2 and Job3 do - update the form created inside worker? If you're getting a CrossThread exception, it means you're updating *some* UI control from the wrong thread - it doesn't necessary have to be the main Form.
Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro