I have a Task inside a function, this is the whole Function:
public async Task CreateRoom(GameTypes game)
{
// Get the user that called this function
User CurrentUser = ConnectedUsers.Single(r => r.Id == Context.ConnectionId);
// Set the user name to Player 1
CurrentUser.Name = "Player 1";
// Add the user to a new list of Users and add that list to the Room
UsersInRoom = new List<User>();
UsersInRoom.Add(CurrentUser);
Room room = new Room() { RoomName = CurrentUser.Name, Game = game, UsersInRoom = UsersInRoom };
AllRooms.Add(room);
// Subscribe the user to the lobby
await Groups.AddToGroupAsync(CurrentUser.Id, CurrentUser.Name);
CurrentUser.Room = CurrentUser.Name;
// Send to the user that the wait screen needs to be opened
await Clients.Caller.SendAsync("OpenWaitScreen");
// Send to all other users to update the lobby list.
await Clients.Others.SendAsync("ForceRoomRequest", game);
// If in 5 minutes no user joins the lobby than send to the caller NoUsersFound
await Task.Delay(300000).ContinueWith(async task =>
{
await Clients.Caller.SendAsync("NoUsersFound");
AllRooms.Remove(room);
});
}
I have found some things on Stackoverflow but i dont know how to implement them.
But I want to be able to cancel this task in a other function.
How would I do that?
EDIT: This is the piece of javascript code that i want to rewrite to C#:
setTimeout(function (){
socket.emit('NoUsersFound');
delete AllRooms[data.room];
}, 300000);