|
Hello,
Thanks for your reply.
Actually my purpose is not to trigger timed event. Ok, just consider the following code,
private void doNothing_Important()
{
int a = 5;
int b = 6;
int c = a + b;
Thread.Sleep(2000);
b = c - 1;
}
Now, in my that code, I want to wait for 2 seconds before executing the last line but If I use Thread.Sleep(2000), then all user interface controls are blocked. An alternative way to perform this task without blocking is to use a thread or backgroudnworker. I can call the Background worker Asynchronously and make a while loop to check if the thread is completed. like this,
private void doNothing_Important()
{
int a = 5;
int b = 6;
int c = a + b;
backGroundWorker1.runWorkerAsyncrhonous();
while(backGroundWorker1.IsBusy)
Application.DoEvents();
b = c - 1;
}
backGroundWorker1_DoWork(BWEventargs e.....)
{
Thread.Sleep(2000);
}
Now, My question is, IS there any other better short cut way to perform the same sleeping task ?
Regards
emran
|
|
|
|
|
You're using the wrong approach.
While a method is being executed in your UI thread, the UI will not respond.
Using Application.DoEvents() is just a workaround for certain cases and shouldn't be used in this case.
The right way IMO would be to put b = c - 1; in a separate method and call this method when a timer elapses.
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
Maybe somthing like this?
int b;
int c;
private void doNothing_Important(){
int a = 5;
b = 6;
c = a + b;
System.Windows.Forms.Timer tim = new System.Windows.Forms.Timer();
tim.Tick += new EventHandler(tim_Tick);
tim.Interval = 2000;
tim.Start();
}
private void tim_Tick(object sender, EventArgs e){
System.Windows.Forms.Timer tim = (System.Windows.Forms.Timer)sender;
tim.Stop()
tim.Tick -= new EventHandler(tim_Tick);
tim.Dispose();
b = c - 1;
}
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.."
-- Mark McCormick || Fold With Us! || Pensieve || VG.Net ||
|
|
|
|
|
Thanks Marc,
Although, I thought, Application.DoEvents() was equivalent to nonBlocking Sleep function. BEcause, I used following snippet whereever I needed to wait for asynchronous methods, i.e.,
while (webBrowser1.ReadyState != ReadyState.Complete)
Application.doEvents();
Now, according to your solution, I will have to split up my methods in timers. Anyway, if C# doesnot provide any more simpler way... what can I do ,
Thanks again,.
Emran
|
|
|
|
|
Hehe you're welcome
DoEvents sort of flushes all the windows messages (from the controls and stuff). A method usually isn't supposed to 'hang' for 2 seconds. Like the poster above me said, DoEvents is mostly a workaround, don't use it too often.
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.."
-- Mark McCormick || Fold With Us! || Pensieve || VG.Net ||
|
|
|
|
|
[Marc] wrote: DoEvents is mostly a workaround
Thanks.
I did not get the term "WorkAround". What does it mean ! Can you give me an example ?
Thanks and regards
|
|
|
|
|
Sorry, can't give an example, i never had to use it as fart as i remember.
A workaround[^] is, as the name states it, that you work around a bug or other weird thing instead of solving the bug/weird thing. Sometimes you can't solve that problem because it's not your fault, and you dont have access to the problems code and so on. Then you have to work around it.
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.."
-- Mark McCormick || Fold With Us! || Pensieve || VG.Net ||
|
|
|
|
|
I have a C#.Net program that once a button is clicked it goes to a public class(called Tal) and gets data in a vector, then exits the class, and goes back to the main form(program). I will do this task over and over. I want to take the data ,do many(over 20) calculations, and go back to the program. How can I get access to the data array(vector) from the main program? I keep getting different errors and have tried so many different ways. Thanks in advance.
Code:
{
Mainfrm
tal.talbars();
}
Public Class tal
// get vector array
Price[] closes = row["SETTLE"] == null ? null : row SETTLE"].PriceVectorValue;
int first_element = elements - 5; // to get 5 back
for (int i = first_element; i < elements; i++)
static void talbars
tal();
So I want to use closes[1-5] in the main program. I get not in namespace error(I put the whole class in the main form, so that shouldnt be an issue). I get name doesnt exist in class also. Any ideas would be greatly appreciated.
|
|
|
|
|
I will make it more basic.
Namespace Program
{
Main()
{
class.tal; // goto class to get data
double calc;
calc = data[1]+data[2];
Console.Writeline(calc);
} //end of Main
Public Class Class()
public void tal
{
// get data for data[1] and data[2]
} // end of class.tal
} // end namespace
I will have alot of data[] and a lot of calcs to be done in the main(cant be done in class). So I get errors when I try and use data[] in Main. Im new to C# and I know its something simple, I just cant figure it out. Thanks again.
|
|
|
|
|
how can i change textbox width at runtime. I want to drag the textbox like v do while designing.
AjayO@PMAM.com
|
|
|
|
|
ajayo wrote: how can i change textbox width at runtime
I would guess you could use the Size property.
|
|
|
|
|
thanks but as i mentioned I want to be able to drag right side of the textbox and change size like v do it in designer.
|
|
|
|
|
Hi Ajay,
did u got the solution for ur problem?
we can write the code to change the textbox width at runtime, but i don't know how to write the code to allow the user to drag the textbox. please whenever u find the solution, try to post the code here.
thanks,
Rahi
|
|
|
|
|
Hi, I know that it's possible to set the ProcessorAffinity in Diagnostics.Process to use only one processor, but how can I do this for a forms application?
|
|
|
|
|
Get a process object for your form and use the ProcessorAffinity member.
Rules of thumb should not be taken for the whole hand.
|
|
|
|
|
Thanks for the suggestion, it sounds logical and I was figuring the same. I haven't found a way to retrieve a process object from a form, however. There's no property or method that lets me fetch it. Would anyone happen to know how this is done?
|
|
|
|
|
There's a static method Process.GetCurrentProcess() you can use.
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
Found it, thanks a bunch!
|
|
|
|
|
I have a requirement in one of our project, we will upload high resolution files to server, but when rendering/downloading the file, user may not require that much high resolution file which takes much time in downloading. For this we have download options,where user can select download size, resolution, after applying this size and resolution to the actual file we have to download the file. can any one please tell me in .Net required code to apply this size and resolution to an image?
|
|
|
|
|
I am running the application using app domain and get the updated version of the current running application using app updater.dll.
The application updated successfully but it not show properly, it closed immediately after updated?
Why the application close immediately.? I want display the updated application.
Please give solution for this problem.
Thanks
|
|
|
|
|
Hi I am trying to get a list of all dates that are between 2 dates so if the dates are
01/08/06 to 06/08/06
the result that I would hope to get is something like this
02/08/06
03/08/06
04/08/06
05/08/06
But my main problem is that I need to count amount of days a player has missed through injury but if the player has 2 injuries at the same time then certain overlapping days are counted twice like these dates here
08/01/2006 to 08/15/2006 14 days
08/01/2006 to 08/25/2006 24 days
I am trying to fill a table with all days and then remove duplicates but I would appreciate any help thanks in advance
Tim
|
|
|
|
|
This sounds suspiciously like a homework question to me.
Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world."
Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that."
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
have not done homework in a few years now but ill freely admit its work for college and is a bit of a struggle
|
|
|
|
|
Why dont you show us what you've already tried, and specifically what problems you are experiencing, and im sure someone will help you out with your homecollege work.
|
|
|
|
|
well my main problem is i dont know if its possible and whether i shud use sql or c#
i can get a count of days between the datesin sql but thats not much good to me really as it doesnt take care of the duplicate days problem
|
|
|
|