|
Create a new thread to process time-consuming tasks.
Here is a simple example
using System.Threading;
...
ThreadStart ts = new ThreadStart(DoWork);
Thread t = new Thread(ts);
t.Start();
...
void DoWork()
{
// do my work here
}
Check out more at at Threading articles on here or MSDN Lib.[^]
Jup
|
|
|
|
|
I use to do it like this:
class SomeClass<br />
{<br />
public Routine1()<br />
{<br />
System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;<br />
<br />
workDone = false;<br />
<br />
ThreadStart ts = new ThreadStart(DoWorkWithStatusChecking);<br />
Thread t = new Thread(ts);<br />
t.Start();<br />
<br />
while( ! t.IsDone this.workDone )<br />
{<br />
System.Windows.Forms.Application.DoEvents();<br />
Sleep(0);<br />
}<br />
}<br />
<br />
bool workDone = false;<br />
<br />
void DoWorkWithStatusChecking()<br />
{<br />
if( this.Status )<br />
{<br />
}<br />
else<br />
{<br />
}<br />
<br />
this.workDone = true;<br />
}<br />
<br />
bool CheckStatus()<br />
{<br />
<br />
return ( true | false );<br />
}<br />
}
Note: routine1 is not reenterable and must be guarded by some sort of guard condition in the beginning!
got the idea?
Alexei.
|
|
|
|
|
hi, i want to creat a custom database which will read data from an unknown file format(made by me) and it will not use any database server...how will i do it? plz help me to solve that..
|
|
|
|
|
|
Hi,
I have the following problem.
When the dialog opens I set up a bunch of combos with database values. After loading them, I set a default value. This all works except for 1 combo (first value gets selected). The strange thing however, is that I have a reset button, which does the exact same thing and then suddenly that default value is set correct.
That combobox is loaded dependent on the contents of the previous one, but I'm sure he does not come in the selectedindex_changed event after the default value was set. (I checked this).
I also compared the properties of the different combo boxes. They are the same.
Can anybody help me on this issue?
Thanks !
I've found a living worth working for, but I haven't found work worth living for.
<marquee>
|
|
|
|
|
I have a function in cpp file returning int * array and taking int * array as a parameter. It is as:
int * funcArg(int *a ) {
int DLLArg [] = {100,200,300};
a=DLLArg;
return a;
};
I have exported this function in the dll (i have checked it with dumpbin.exe)
But I can not handle this function in c# client. My code in c# is as: ( ıt gives this error. "An unhandled exception of type 'System.Runtime.InteropServices.SafeArrayRankMismatchException' occurred in CSharpClient.exe
Additional information: Safe array of rank 51241 has been passed to a method expecting an array of rank 1."
What should I do? URGENT help!!!
namespace CSharpClient
{
class Class1
{
[DllImport("DenemeNonMfcDll.dll",EntryPoint="?Sub@DLLclass@@QAEHHH@Z")]
public extern static int Sub(int a, int b);
[DllImport("DenemeNonMfcDll.dll")]
[return: MarshalAs(UnmanagedType.SafeArray)]
public extern static int [] funcArg(int [] a);
public static int [] intArray = new int [3] {0,1,2} ;
static void Main(string[] args)
{
foreach (int num in funcArg(intArray) )
{
System.Console.WriteLine(num.ToString());
}
Console.WriteLine("Press any key to continue...");
Console.ReadLine();
}
}
}
|
|
|
|
|
Hello Everybody,
I have an application running in a Citrix MetaFrame. That application automates Outlook so read the user's inbox and send emails. I create and release the outlook instance like that:
Application outlook = new ApplicationClass();
try
{
DoSomething(outlook);
}
finally
{
Marshal.ReleaseComObject(outlook);
outlook = null;
}
[EDIT]I cannot call outlook.quit() , because it would also close all local instances, if you run the application locally (outside MetaFrame).[/EDIT]
It works fine. The users run the application in the MetaFrame and their visible Outlook instances locally on their desktops.
The only problem is: They forget going to meetings, because Outlook doesn't show them any notifications a few minutes before an Appointment starts. The remote Outlook (running invisibly in the MetaFrame) prevents the local Oulook from showing the notifications.
For example, you add an appointment to your calendar folder and configure a reminder 10 minutes before starting time. So, you expect Outlook's notify-window to pop up ten minutes before the appointment.
It does pop up, if nothing is running via Citrix at the moment. But if my application is running, there will be no notify-windows, the local Outlook instance won't show any reminders.
What is the problem? Does anyone know why the calendar reminders don't work locally, while there's another instance running in a Citrix MetaFrame? Please tell me...
Thanks in advance,
coco
_____________________________________________________________________________
I don't expect too much, all I want is your vote for Halbsichtigkeit.
-- modified at 4:03 Friday 7th July, 2006
|
|
|
|
|
Why values are not same? I simple want to find the position of Cursor through LParam. But calculated values are different.
<br />
Point screenMousePointOld = Control.MousePosition;<br />
Point screenMousePoint = new Point(LoWord(message.LParam.ToInt32()), HiWord(message.LParam.ToInt32()));<br />
<br />
private static int HiWord(int number)<br />
{<br />
if ((number & 0x80000000) == 0x80000000)<br />
return (number >> 16);<br />
else<br />
return (number >> 16) & 0xffff ;<br />
}<br />
<br />
private static int LoWord(int number)<br />
{<br />
return number & 0xffff;<br />
}<br />
What I am doing wrong?
|
|
|
|
|
Since LParam is 64 bit number, so you have to convert it to int64 type.
|
|
|
|
|
hi. i am trying to prevent the form from queueing repeat clicks.
i set the cursor to wait, but when the user clicks on a menu button it queues the command still.
i want it to ignore the command.
how do i do this?
i tried the following:
if(cursor.current == cursors.waitcursor)
return;
cursor.current = cursors.waitcursor;
|
|
|
|
|
Hello
Check the MouseCapture property. Set it to true in an irrelevant control of your form -eg. the statusbar-, this way it will capture the mouse and no other control will respond to hovering or clicking, until you release the capture again by setting it to false in the same control.
Regards
|
|
|
|
|
can you please elaborate?
which mouse capture?
|
|
|
|
|
Hello
There is a Control.Capture property in all controls. When you set it to true in any control, it will capture the mouse and the mouse click won't work until you click on something else once, then the control will lose its capture and the second click will work -remember: the first won't do anything- This will give you a chance to handle the MouseCaptureChanged event of that control to check if you intentionally released the mouse -you might want to use a boolean here-, or if the user clicked somewhere. If so, it will recapture the mouse. Here is a code snippit:
bool Release;
public void MyMethod()
{
Release = false;
UseWaitCursor = true;
MyControl.Capture = true;
UseWaitCursor = false;
Release = true;
MyControl.Capture = false;
}
private void MyControl_MouseCaptureChanged(object sender, EventArgs e)
{
if(!Release)
MyControl.Capture = true;
}
Regards
|
|
|
|
|
how do i associate the method with my control?
|
|
|
|
|
What method?? The MyMethod is your method that does the lengthy operation. you write it from the scratch!! The other one is the event handler. Just select your control, then double click on its MouseCaptureChanged event in the event tab of its property grid.
If you don't already have a control to capture the mouse just make a dummy one:
Control MyControl = new Control();
|
|
|
|
|
i cant seem to get the MouseCaptureChanged event on my dummy control, myControl.
myClass()
{
busy = false;
Control myControl = new Control();
myControl.Capture = false;
}
myLongMethod()
{
myControl.Capture = true;
busy = true;
Cursor.Current = Cursors.WaitCursor;
// work code
Cursor.Current = Cursors.Default;
busy = false;
myControl.Capture = false;
}
btw, i appreciate the help. it probably seems really simple and basic to you, but for some reason i cant get it to work.
|
|
|
|
|
Hello
Forgive me if I say that your code has some mistakes . One of them is that you don't really recognize the scope of objects. if you declare an object in one method, it's destroyed & goes out of scope once the method is over -well, there are more details on this subject-.
Also you must add an event handler for your event to be handled by a method -notice the += operator below-
I'll assume that MyClass is your class, & it's driven from System.Windows.From
class MyClass : Form
{
Control Dummy;
bool busy;
MyClass()
{
Dummy = new Control();
busy = false;
Dummy.MouseCaptureChanged += new EventHandler(Dummy_MouseCaptureChanged);
}
public void MyLongMethod()
{
busy = true;
Dummy.Capture = true;
UseWaitCursor = true;
busy = false;
Dummy.Capture = false;
UseWaitCursor = false;
}
void Dummy_MouseCaptureChanged(object sender, EventArgs e)
{
if (busy)
Dummy.Capture = true;
}
}
Happy programming
|
|
|
|
|
when i compile this code, i get the following error:
System.Windows.Forms.Control' does not contain a definition for 'MouseCaptureChanged
MS development environment 2003 version 7.1.3088
.net framework 1.1 version 1.1.4322 sp1
edit: turns out this event is only in 2.0
-- modified at 1:41 Monday 10th July, 2006
|
|
|
|
|
Hello
lushgrass wrote: turns out this event is only in 2.0
unfortunately that is true. Yet still the Capture property is present in the Framework 1.1 you are using. So instead of using the MouseCaptureChanged event. You may use the Control.LostFocus like this:
1- Change the event handler declaration to this:
Dummy.LostFocus += new EventHandler(Dummy_LostFocus);
2- And the event handler definition into this:
private void Dummy_LostFocus(object sender, System.EventArgs e)
{
if(Busy)
Dummy.Capture = true;
}
Regards
|
|
|
|
|
so the end code should look like the following:
<br />
public class Form1<br />
{<br />
private StatusBar status;<br />
private bool busy;<br />
private Control control;<br />
<br />
public Form1()<br />
{<br />
status.Capture = true;<br />
busy = false;<br />
control = new Control();<br />
control.Capture = false;<br />
control.LostFocus += new EventHandler(control_LostFocus);<br />
}<br />
<br />
private void control_LostFocus()<br />
{<br />
if(busy)<br />
control.Capture = true;<br />
}<br />
<br />
private void Search()<br />
{<br />
status.Text = "Searching";<br />
busy = true;<br />
control.Capture = true;<br />
<br />
<br />
control.Capture = false;<br />
busy = false;<br />
status.Text = "";<br />
}<br />
}<br />
|
|
|
|
|
Hello
Well, something like that. Yet there are two modifications
public class Form1
{
private StatusBar status;
private bool busy;
private Control control;
public Form1()
{
busy = false;
control = new Control();
control.Capture = false;
control.LostFocus += new EventHandler(control_LostFocus);
}
private void control_LostFocus()
{
if(busy)
control.Capture = true;
}
private void Search()
{
status.Text = "Searching";
busy = true;
control.Capture = true;
busy = false;
control.Capture = false;
status.Text = "";
}
}
NB.
Next time use the "pre" instead of "code " formatting
Regards
|
|
|
|
|
for some reason this doesnt resolve my issue.
if i click on search really quickly it still searches in series instead of ignoring the extra clicks.
|
|
|
|
|
If this is your problem simply add
if(busy)
return;
at the beginning of the click event handler (ie. Button1_clicked) or the Search() method
Ragards
|
|
|
|
|
i inserted that code before the actual searching method.
still didnt work.
does it matter if the event is from a menu?
or would any other events throw it off?
edit 1: right now the only solution i can find is to pop up
another form that says there is something being processed.
it wouldnt have any buttons and would lock the user out
until the process succeeds or fails.
edit 2: just tested it and was unsuccessful. the parent
form seems unable to do anything while the child form has
focus.
the problem has expanded slightly to include any action that
requires a period of time from the network.
still hoping for ideas.
-- modified at 1:07 Thursday 13th July, 2006
|
|
|
|
|
Hello
There are many ways of dealing with this problem -lengthy operations- and each programmer chooses what suits him. Just at a glance I can think of some:
1- Put the operation on a separate thread and never stop dealing with the user
2- Disable all child controls using foreach loop in the parent
3- Override click event handlers in all child controls to display a gentle message explaining why the form is not responding
4- Displaying a modal form that captures the focus with a ProgressBar, Statusbar, and a cancel button.
PS.
I've tested the code I gave you, and it works fine. The problem must be in your implementation.
Regards
|
|
|
|