By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,887 Members | 1,148 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,887 IT Pros & Developers. It's quick & easy.

C# Determine if the current thread is the main GUI thread.

P: 81
Hey All,
Just curious, does anyone know if there is a way to check if the current executing thread is the main WPF GUI thread?

I want to have it so that a non-threaded remoting call will fail if the current thread is the WPF GUI thread. The reason for this is that a long running call locks up the GUI, and during this time, we would like it to stay responsive.

Does checking to see if it's a background thread do the trick?

Thanks!
Jun 26 '08 #1
Share this Question
Share on Google+
15 Replies


P: 81
Anyone have any ideas?
Jun 26 '08 #2

RedSon
Expert 5K+
P: 5,000
Can you do it with the System.Reflection namespace?
Jun 26 '08 #3

P: 81
hmmmm how would that work?
Jun 26 '08 #4

RedSon
Expert 5K+
P: 5,000
hmmmm how would that work?
Not sure did you check it out?
Jun 27 '08 #5

P: 81
Well, the thing is I have no idea how reflection would allow me to get the current main thread. It really doesn't make sense that I could get the applications GUI thread via reflection, since reflection is used to instantiate or use a class remotely.
Jun 27 '08 #6

RedSon
Expert 5K+
P: 5,000
Well, the thing is I have no idea how reflection would allow me to get the current main thread. It really doesn't make sense that I could get the applications GUI thread via reflection, since reflection is used to instantiate or use a class remotely.
Are you just trying to get a handle to the main process thread or the currently active window?
Jun 27 '08 #7

P: 81
Are you just trying to get a handle to the main process thread or the currently active window?
hmmm, well the here is the challange:

We have calls the we make for remoting over http. We do not want these calls to hang the UI. So we need to make the call, and still allow the UI to not lock up. So we need to spawn a separate thread correct?
If so, once we get the data back, I believe we need to then allow UI updates. How does one go about this in C#? I figured if we had a handle on the gui thread we can just call BeginInvoke?
Jun 27 '08 #8

P: 57
hmmm, well the here is the challange:

We have calls the we make for remoting over http. We do not want these calls to hang the UI. So we need to make the call, and still allow the UI to not lock up. So we need to spawn a separate thread correct?
If so, once we get the data back, I believe we need to then allow UI updates. How does one go about this in C#? I figured if we had a handle on the gui thread we can just call BeginInvoke?
Can you just name all your threads? then check thread.Name
Jun 27 '08 #9

P: 57
Can you just name all your threads? then check thread.Name
Is this helpful?

You can use BeginInvoke to invoke a delegate in the UI as soon as the thread doing your call is completed.

first define the delegate. here is an example.

public delegate void mydelegate(string aparameter);

mydelegate del = new mydelegate(somefunction);

then in the thread BeginInvoke(del,new object[]{"im done"});
Jun 27 '08 #10

RedSon
Expert 5K+
P: 5,000
Is this helpful?

You can use BeginInvoke to invoke a delegate in the UI as soon as the thread doing your call is completed.

first define the delegate. here is an example.

public delegate void mydelegate(string aparameter);

mydelegate del = new mydelegate(somefunction);

then in the thread BeginInvoke(del,new object[]{"im done"});
I think you can also accomplish this with message queues, if those exist in C#. You create another thread then pass messages between them.
Jun 27 '08 #11

P: 81
I think you can also accomplish this with message queues, if those exist in C#. You create another thread then pass messages between them.
We are actually using it currently by using begin invoke. It's really the only way I could see it working. However, in one part, we actually want to check to see if they did call begin invoke. If they didn't then they are running on the main thread and we throw an exception. Maybe naming them would work?
Jun 27 '08 #12

P: 1
private void DoSomething(string message)
{
if (this.InvokeRequired && this.IsHandleCreated && !this.IsDisposed)
{
this.Invoke(new MethodInvoker(delegate
{
DoSomething(message);
}));
}
else
{
// your code here
}
}
Jan 15 '10 #13

Plater
Expert 5K+
P: 7,872
Application.MessageLoop
"Gets a value indicating whether a message loop exists on this thread."
Could probably do that yeah?
Jan 15 '10 #14

tlhintoq
Expert 2.5K+
P: 3,525
hmmm, well the here is the challange:

We have calls the we make for remoting over http. We do not want these calls to hang the UI. So we need to make the call, and still allow the UI to not lock up. So we need to spawn a separate thread correct?
If so, once we get the data back, I believe we need to then allow UI updates. How does one go about this in C#? I figured if we had a handle on the gui thread we can just call BeginInvoke?
Another thought is that you don't need to be able to identify the GUI thread at all. Maybe you are making more work than you need to.
Spawn a new thread to do the task (from the GUI thread/class or some other thread/class), and register for an event that is inside the class you spawn.
When that second thread completes its task it then raises an event (optionally with eventargs that contain data you need to pass back to the first class). It doesn't know or care who is listening, because you don't want or need it to be tightly bound. The first thread hears this event and reacts accordingly (which can include disposing of the second class, unregistering from events it throws etc)
Events tutorial (including Form to Form which is the same as class to class)
Jan 24 '10 #15

P: 1
I use CheckAccess() or VerifyAccess()
Jun 7 '12 #16

Post your reply

Sign in to post your reply or Sign up for a free account.