I am working o开发者_Go百科n an application that interacts with a database via stored procedures. To prevent the GUI being unresponsive i run the stored procedures each in a different thread. I tested this with a loop in a thread and that works, but when i call a stored procedure which has to return very much records, the GUI is still blocking.
Now my question is: can a context switch occur when a method call (the stored procedure in this case) is not finished yet? I know that c# code is compiled to IL. Is it possible that an IL-instruction is getting all the Threading resources instead of the GUI thread ? When occurs a context switch to another process and is it different from context switching to different threads belonging to the same process ?
Edit:
This is how i start the thread:
private void button2_Click(object sender, EventArgs e)
{
filldb = new Thread(adddummytodb);
filldb.Start();
}
This is the code in the thread with the stored procedure call. When i run this, the GUI hangs directly however i know that the stored procedure call lasts a noticable time because it gets +100000 records. When does the thread end ? When the invoked filldgv method is called or when it is finished ?
private void adddummytodb()
{
Eigenaars = dc.sp_SearchEigenaar("", "", "", null, null);
this.dataGridView1.Invoke((controlInvoke)filldgv);
When i Run this code with a loop of short duration stored procedure calls, the GUI stays responsive:
private void adddummytodb()
{
int ts;
for (int i = 0; i < 2000; i++)
{
if (stopthread == false)
{
ts = dc.sp_SaveCst(0);
}
else
break;
}
can a context switch occur when a method call (the stored procedure in this case) is not finished yet?
Yes. Context switches occur all the time and more often than you probably think.
Is it possible that an IL-instruction is getting all the Threading resources instead of the GUI thread ?
Technically speaking your IL code is translated to native machine code and this is what gets executed by your CPU. An instruction can't "get all threading resources". Your CPU just constantly executes code, it has no knowledge of threads, processes etc. It's the operating system that organizes things in that way and takes care of running those threads in turn for a specific time slice.
When occurs a context switch to another process?
Context switches normally occur between threads. Processes are not run, it's their threads that receive execution time. A context switch can happen any time - when the time slice for the thread is over, it goes to sleep waiting for something, there is an interrupt and an interrupt handler needs to be executed.
is it different from context switching to different threads belonging to the same process
Like I said context switching occurs between threads. The next thread to be executed might be from your process or from some other process.
My guess is that you wait from your GUI thread for the async operation (second thread executing an SP) to complete in some way - you block. You could have done SP execution on the GUI thread to begin with - the result is the same. The solution is for your second thread to call into the GUI thread once its complete.
For example the BackgroundWorker offers a special event for this (RunWorkerComplete). Otherwise you can do it using Control.Invoke/BeginInvoke or SynchronizationContext.Post/Send.
Your code will always be in a method call of some description, even if that method is the entry point to your app, so yes, obviously context switching happens during the execution of a method.
I suspect knowing this will not help you much. It sounds to me like you UI is still blocking on the results from the database. If you can post a code sample that may help, or take a look at an introductory example on threading and UIs, e.g. http://msdn.microsoft.com/en-us/library/ywkkz4s1.aspx
A context switch can occur on all operations that are not defined as atomic . In some languages there are special atomic functions that are guaranteed to execute in one step. These functions usually are assign an int to some other int or something of that extent.
Otherwise a context switch can always occur, there is no way to prevent this.
See MSDN at http://msdn.microsoft.com/en-us/library/5kczs5b5(v=VS.71).aspx
Some more infos: http://www.eggheadcafe.com/software/aspnet/32045281/atomic-operations.aspx or http://blog.drorhelper.com/2008/09/why-atomic-operations-are-not-always.html
As for your problem: I am heading into the same direction as forsvarir, I assume your long running thread directly writes back into Form-component. This will not work, use Invoke.
hth
Mario
精彩评论